(精華)2020年7月22日 ASP.NET Core Swagger的使用(NSwag工具版)

愚公搬程式碼發表於2020-07-22

首先安裝包

Install-Package NSwag.AspNetCore
public void ConfigureServices(IServiceCollection services)
{
   //swagger
	services.AddOpenApiDocument(settings =>
	{
	    settings.AddSecurity("身份認證Token", Enumerable.Empty<string>(), new OpenApiSecurityScheme()
	    {
	        Scheme = "bearer",
	        Description = "Authorization:Bearer {your JWT token}<br/><b>授權地址:/Base_Manage/Home/SubmitLogin</b>",
	        Name = "Authorization",
	        In = OpenApiSecurityApiKeyLocation.Header,
	        Type = OpenApiSecuritySchemeType.Http
	    });
	});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   app.UseOpenApi(); //新增swagger生成api文件(預設路由文件 /swagger/v1/swagger.json)
   app.UseSwaggerUi3();//新增Swagger UI到請求管道中(預設路由: /swagger).
}

相關文章