1.首先先去工具->NuGet套件管理員->管理方案NuGet套件->安裝Swashbuckle.AspNetCore
2.開啟Startup.cs檔案在using加入
using Microsoft.OpenApi.Models;
using System;
using System.Reflection;
using System.IO;
3.在public void ConfigureServices(IServiceCollection services) 加入
#region 添加Swagger
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath, true);
});
#endregion
4.在public void Configure(IApplicationBuilder app, IWebHostEnvironment env)加入
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API");
});
5.在專案->專案屬性->建置下點選XML文件檔案
6.結果
Web Api使用Swagger 就這麼簡單