https://github.com/RSuter/NSwag/wiki#ways-to-use-the-toolchaingit
Swagger generation:github
You can customize the Swagger generator with the following extension points of NSwag:json
https://github.com/RSuter/NSwag/wiki/OWIN-Middlewareapi
The NuGet package provides extension methods to register the NSwag ASP.NET OWIN middlewares:app
The middlewares are based on WebApiToSwaggerGenerator - the old reflection based ASP.NET Core and ASP.NET generatoride
Swagger only:visual-studio
app.UseSwagger(assembly, configure)
: Registers the Swagger generator on a given routeSwagger and Swagger UI: (用了這個,就不能用上面的,是衝突的)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 routesThe default routes to access the Swagger specification or Swagger UI:this
http://yourserver/swagger/v1/swagger.json
http://yourserver/swagger
First, you need to install the required NSwag NuGet packages.spa
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:
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)來處理文檔