.NET Core和Swagger 生成 Api 文檔

1 nuget  添加  Swashbuckle.AspNetCorejson

2 修改startup.cs文件api

命名空間瀏覽器

using Swashbuckle.AspNetCore.Swagger;app

ConfigureServices方法加入AddSwaggerGen:spa


//Inject Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "MyApi", Version = "v1" });
// Set the comments path for the Swagger JSON and UI.
var xmlPath = Path.Combine(AppContext.BaseDirectory, "項目名稱.XML");
c.IncludeXmlComments(xmlPath);
});xml


Configure方法加入,容許Swagger提供服務生成json文檔以及UI:文檔

app.UseSwagger().UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});get

it

3.設置XML註釋
在 Visual Studio 中右擊項目而且選擇 屬性在 生成 區域下面勾選 XML文檔文件 。io

填入 項目名稱.XML

4 給 api添加註釋

/// <summary>

/// Put value by id and value

/// </summary>

/// <param name="id">id</param>

/// <param name="value">value</param>

5 生成 json文件

在瀏覽器中輸入:http://localhost:端口/swagger/v1/swagger.json

顯示json內容

6.在瀏覽器輸入:http://localhost:9040/swagger/ 

相關文章
相關標籤/搜索