iT邦幫忙

2021 iThome 鐵人賽

DAY 28
0
自我挑戰組

C# 學習之旅系列 第 28

ASP.NET MVC 從入門到放棄(Day28)- MVC web api 加入swagger介紹

  • 分享至 

  • twitterImage
  •  

1.首先先去工具->NuGet套件管理員->管理方案NuGet套件->安裝Swashbuckle.AspNetCore
https://ithelp.ithome.com.tw/upload/images/20210907/20140001jHX872585N.png

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

https://ithelp.ithome.com.tw/upload/images/20210907/20140001qcXXMBqtzi.png

4.在public void Configure(IApplicationBuilder app, IWebHostEnvironment env)加入

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

https://ithelp.ithome.com.tw/upload/images/20210907/20140001mrIcwu6qlv.png

5.在專案->專案屬性->建置下點選XML文件檔案
https://ithelp.ithome.com.tw/upload/images/20210907/201400017ibV1SRlPQ.png

6.結果
https://ithelp.ithome.com.tw/upload/images/20210907/20140001bo8inIsTZV.png

Web Api使用Swagger 就這麼簡單/images/emoticon/emoticon18.gif


上一篇
ASP.NET MVC 從入門到放棄(Day27)-IIS 發佈介紹
下一篇
ASP.NET MVC 從入門到放棄(Day29)-MVC 實作一個web api
系列文
C# 學習之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言