🚀 Launch Week Day 5:Introducing Immutable Scans.Learn More
Socket
Book a DemoInstallSign in
Socket

ZJW.NancySwagger

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ZJW.NancySwagger

源码来自https://github.com/yahehe/Nancy.Swagger 本人仅仅只是做了局部修改,使用方式参考gitee

nugetNuGet
Version
2.0.2
Version published
Maintainers
1
Created
Source

Nancy.SwaggerAnnotations

Nancy一款轻量级的WebAPI框架,可以使用自宿主方式运行。

NancySwagger可以使Nancy支持Swagger

通过简单的特性注解,即可完成文档配置生成

浏览器输入 http://127.0.0.1:{端口号}/swagger 可访问文档

新增多语言支持,传入多语言字典 SwaggerConfig.SetLanguage(languageDict);

dotnet add package ZJW.NancySwagger --version 2.0.2

using Nancy;
using Nancy.Bootstrapper;
using Nancy.Conventions;
using Nancy.Swagger.Annotations;
using Nancy.Swagger.Services;
using Nancy.TinyIoc;
using Swagger.ObjectModel; 

namespace NancyTest
{
    public class ApplicationBootstrapper : DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            SwaggerMetadataProvider.SetInfo("API", "v1.0", "My API", new Contact
            {
                EmailAddress = "xxxxxxx@qq.com",
                Name = "大师兄法号随缘",
                Url = "https://www.cnblogs.com/zjwno1"
            }, "http://www.cnblogs.com/zjwno1");
            base.ApplicationStartup(container, pipelines); 
            //仅显示有特性的接口
            SwaggerAnnotationsConfig.ShowOnlyAnnotatedRoutes = true;
			
        }

        protected override void ConfigureConventions(NancyConventions nancyConventions)
        {
            //... 配置你自己的静态文件等
            base.ConfigureConventions(nancyConventions);
        }

        public override void Configure(INancyEnvironment environment)
        { 
            //保留属性原始的命名方式,下面两个都得配置,默认都是首字母小写的
            SwaggerAnnotationsConfig.RetainCasing = true;
            environment.Json(defaultEncoding: System.Text.Encoding.UTF8, retainCasing: true);
            base.Configure(environment);
        }

        /// <summary>
        /// 允许跨域
        /// </summary>
        /// <param name="container"></param>
        /// <param name="pipelines"></param>
        /// <param name="context"></param>
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Origin", "*"));
            pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Headers", "*"));
            pipelines.AfterRequest.AddItemToEndOfPipeline(x => x.Response.Headers.Add("Access-Control-Allow-Methods", "*"));
        }
    }
}
 


using Nancy;
using Nancy.ModelBinding;
using Nancy.Swagger;
using Nancy.Swagger.Annotations.Attributes;
using NancyTest.Models;
using NancyTest.Utility.Extension;
using Swagger.ObjectModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace NancyTest
{
    public class HomeModule : NancyModule
    {
        

        public HomeModule(ISwaggerModelCatalog modelCatalog)
        {
            //涉及到哪些Model
            modelCatalog.AddModel<RetData>();
            modelCatalog.AddModel<UploadData>();

            //路由
            Get("/getData", dynamic => Index(), null, "GetData");
            Get("/queryData", dynamic =>
            {
                string userName = Request.Query.userName ?? "";
                return QueryData(userName);
            }, null, "QueryData");
            Post("/postForm", dynamic => UploadData(dynamic.Id, dynamic.Name), null, "PostForm");
            Post("/uploadFile", dynamic =>
            {
                IEnumerable<HttpFile> files = Request.Files;
                if (files == null || files.Count() == 0)
                    return UploadFile(null);
                return UploadFile(files.FirstOrDefault());
            }, null, "UploadFile");
            //这个就不需要Swagger显示了,不需要注解
            Get("/upload/{name*}", dynamic => ShowUploadFile(dynamic.name ?? ""), null, "GetUploadFile");
            Post("/postJson", dynamic =>
            {
                UploadData config = this.Bind<UploadData>();
                return UploadJson(config);
            }, null, "PostJson");
        }

        private object ShowUploadFile(string name)
        {
            string contentType = MimeTypes.GetMimeType(name);
            return Response.AsFile(MyEnvironment.Root("/Upload/" + name), contentType);
        }

        [Route("GetData")]//Name
        [Route(HttpMethod.Get, "/getData")]//Method And Path
        [Route(Summary = "获得XX数据")]
        [SwaggerResponse(HttpStatusCode.OK, typeof(RetData))]
        [Route(Tags = new[] { "获得XX数据Tag" })]
        private object Index()
        {
            return Response.AsJson(new RetData { state = 1, message = "Hello World!" });
        }

        [Route("QueryData")]//Name
        [Route(HttpMethod.Get, "/queryData")]//Method And Path
        [Route(Summary = "获得某用户的数据")]
        [SwaggerResponse(HttpStatusCode.OK, typeof(RetData))]
        [Route(Tags = new[] { "获得某用户的数据Tag" })]
        private object QueryData([RouteParam(ParameterIn.Query, Name = "用户名")] string userName)
        {
            return new RetData { state = 1, message = "查询成功", data = "用户XX,年龄18" };
        }




        [Route("PostForm")]//Name
        [Route(HttpMethod.Post, "/postForm")]//Method And Path
        [Route(Summary = "上传XX数据")]
        [SwaggerResponse(HttpStatusCode.OK, typeof(RetData))]
        [Route(Tags = new[] { "上传XX数据Tag" })]
        private object UploadData([RouteParam(ParameterIn.Form, Name = "ID")] string id, [RouteParam(ParameterIn.Form, Name = "姓名")] string name)
        {
            return Response.AsJson(new RetData { state = 1, message = "上传成功" });
        }


        [Route("UploadFile")]//Name
        [Route(HttpMethod.Post, "/uploadFile")]//Method And Path
        [Route(Summary = "文件上传")]
        [SwaggerResponse(HttpStatusCode.OK, typeof(RetData))]
        [Route(Tags = new[] { "文件上传Tag" })]
        private object UploadFile([RouteParam(ParamIn = ParameterIn.Form, Name = "file", Description = "文件", ParamType = typeof(SwaggerFile), Required = true)] HttpFile file)
        {
            //保存文件
            string uploadPath = MyEnvironment.Root("/Upload/");
            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            string ext = Path.GetExtension(file.Name);
            string fileName = Guid.NewGuid().ToString().Replace("-", "") + ext;
            //完整路径
            var filePath = uploadPath + fileName;
            using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
            {
                file.Value.CopyTo(fileStream);
            }
            var virtualPath = "/Upload/" + fileName;
            //返回虚拟路径
            return Response.AsJson(new RetData { state = 1, message = "上传成功", data = virtualPath });
        }




        [Route("PostJson")]//Name
        [Route(HttpMethod.Post, "/postJson")]//Method And Path
        [Route(Summary = "上传XXX数据")]
        [SwaggerResponse(HttpStatusCode.OK, typeof(RetData))]
        [Route(Tags = new[] { "上传XXX数据Tag" })]
        private object UploadJson([RouteParam(ParameterIn.Body, Name = "JSON数据上传")] UploadData config)
        {
            return Response.AsJson(new RetData { state = 1, message = "上传成功" });
        }
    }
}

Keywords

FAQs

Package last updated on 26 Nov 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts