
Security News
crates.io Ships Security Tab and Tightens Publishing Controls
crates.io adds a Security tab backed by RustSec advisories and narrows trusted publishing paths to reduce common CI publishing risks.
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 = "上传成功" });
}
}
}
FAQs
源码来自https://github.com/yahehe/Nancy.Swagger 本人仅仅只是做了局部修改,使用方式参考gitee
We found that zjw.nancyswagger demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
crates.io adds a Security tab backed by RustSec advisories and narrows trusted publishing paths to reduce common CI publishing risks.

Research
/Security News
A Chrome extension claiming to hide Amazon ads was found secretly hijacking affiliate links, replacing creators’ tags with its own without user consent.

Security News
A surge of AI-generated vulnerability reports has pushed open source maintainers to rethink bug bounties and tighten security disclosure processes.