定制swagger的UI
https://github.com/RSuter/NSwag/wiki#ways-to-use-the-toolchain
Customizations
Swagger generation:
You can customize the Swagger generator with the following extension points of NSwag:
https://github.com/RSuter/NSwag/wiki/OWIN-Middleware
- Package: NSwag.AspNet.Owin (.NET 4.5+)
The NuGet package provides extension methods to register the NSwag ASP.NET OWIN middlewares:
The middlewares are based on WebApiToSwaggerGenerator - the old reflection based ASP.NET Core and ASP.NET generator
Swagger only:
app.UseSwagger(assembly, configure)
: Registers the Swagger generator on a given route
Swagger and Swagger UI: (用了这个,就不能用上面的,是冲突的)
app.UseSwaggerUi(assembly, configure)
: Registers the Swagger generator and Swagger UI v2.x on the given routesapp.UseSwaggerUi(configure)
: Registers only the Swagger UI on the given routeapp.UseSwaggerUi3(configure)
: Registers the Swagger generator and Swagger UI v3.x on the given routesapp.UseSwaggerReDoc(configure)
: Registers the Swagger generator and ReDoc on the given routes
The default routes to access the Swagger specification or Swagger UI:
- Swagger JSON:
http://yourserver/swagger/v1/swagger.json
- Swagger UI:
http://yourserver/swagger
1. Install required NuGet packages
First, you need to install the required NSwag NuGet packages.
2. Register the middleware
public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
app.UseSwaggerUi(typeof(Startup).Assembly, settings =>
{
// configure settings here
// settings.GeneratorSettings.*: Generator settings and extension points
// settings.*: Routing and UI settings
});
app.UseWebApi(config);
config.MapHttpAttributeRoutes();
config.EnsureInitialized();
}
}
Configure the routing of the Swagger requests
There are two ways to do this:
自定义
Post process the served Swagger specification
It is possible to transform the generated Swagger specification before it is served to the client:
app.UseSwagger(typeof(Startup).Assembly, settings =>
{
settings.PostProcess = document =>
{
document.Info.Description = "My description";
};
});
If you want to implement more reusable code, you can also implement Document Processors and Operation Processors.
UseSwagger和UseSwaggerUi3只能用一个
app.UseSwaggerUi3(typeof(Startup).Assembly, settings => { // configure settings here // settings.GeneratorSettings.*: Generator settings and extension points // settings.*: Routing and UI settings settings.GeneratorSettings.DefaultUrlTemplate = "api/{controller}/{action}/{id}"; settings.PostProcess = document => { document.Info.Title = "My Services"; document.Info.Version = "1.5"; document.Info.Contact = new SwaggerContact {Name = "My team", Email = "test@qq.com" }; }; });
参考
https://github.com/RSuter/NSwag/blob/master/src/NSwag.Sample.NETCore22/Startup.cs 这里有ConfigureServices的方法签名示例
https://docs.particular.net/samples/dependency-injection/aspnetcore/ 需要引用Autofac.Extensions.DependencyInjection
https://github.com/RSuter/NSwag/wiki/Middlewares 两种不同的middleware
https://github.com/RSuter/NSwag/wiki/AspNetCore-Middleware 这个里面是用ConfigureServices来处理文档
https://github.com/RSuter/NSwag/wiki/OWIN-Middleware 这个里面是用pp.UseSwaggerUi3(configure)来处理文档
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-01-24 Code Coverage and Unit Test in SonarQube