Skip to content

三步完成webapi文档生成

1.在Webapi项目引用NSwag.AspNetCore

2.Startup中启用NSwag中间件

.netcore 3.0

csharp
services.AddSwaggerDocument(
  config =>
  {
	  config.Title = "交易宝api";
	  config.Description = "交易宝api文档";
	  config.DocumentName = "交易宝";
	  config.Version = "1.0";

	  config.AddSecurity("JWT", Enumerable.Empty<string>(), new NSwag.OpenApiSecurityScheme
	  {
		  Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
		  Name = "Authorization",
		  In = NSwag.OpenApiSecurityApiKeyLocation.Header,
		  Description = "Value: Bearer {Your JWT token}"
	  });
	  config.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT"));
  }
 );

//启用中间件

csharp
  app.UseOpenApi();
  app.UseSwaggerUi3();

.net core 2.0

csharp
public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{

    //启用中间件为生成的 Swagger 规范和 Swagger UI 提供服务
        app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, settings =>
        {
            settings.GeneratorSettings.Title = "RsWebApi项目文档";
            settings.GeneratorSettings.Description = "这是一份关于RsWebApi项目管理的文档";
            settings.GeneratorSettings.Version = "1.0";  
            settings.GeneratorSettings.DefaultPropertyNameHandling =
                PropertyNameHandling.CamelCase;
        });
      
        app.UseMvc();
}

3.访问 http://项目地址/swagger