【轉】C# 在webapi項目中配置Swagger

來源:互聯網
上載者:User

標籤:pre   was   comment   ble   修改   main   start   alc   type   

     以前在做WebAPI調用測試時,一直在使用Fiddler測試載入器了,而且這個用起來比較繁瑣,需要各種配置,並且不直觀,還有一點是還得弄明白URL地址和要傳遞的參數,然後才能調用。  最近新入職,公司裡面也用到了WebAPI調用,但是使用的調用工具讓我眼前一亮,他用的是Swagger。這個工具可能有的人沒聽說過,它僅僅引用一個包即可嵌入到項目中,用的時候直接輸入網址就能看到調用介面了。你是不是之前也沒用過呢,是的話,那請看下面介紹吧~

   1. 首先,建立webpapi類型的項目 TestSwagger

   2.使用 Nuget 安裝Swashbuckle.core包

   安裝完成後,即可看到這些類與檔案都是安裝完成時swagger添加的。

   

  3. 開啟xml文檔檔案

  右鍵項目屬性—>產生—>勾選XML文檔檔案

 

 4.添加註釋 

 我們發現,安裝完成後,寫注釋並沒有在swagger頁面上面增加,所以我們現在開啟注釋

 在SwaggerConfig類中,EnableSwagger的時候添加下面XML解析

using System.Web.Http;using WebActivatorEx;using TestSwagger;using Swashbuckle.Application;[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]namespace TestSwagger{    public class SwaggerConfig    {        public static void Register()        {            var thisAssembly = typeof(SwaggerConfig).Assembly;            GlobalConfiguration.Configuration                 .EnableSwagger(c =>                    {                        c.SingleApiVersion("v1", "TestSwagger");                        //添加XML解析                        c.IncludeXmlComments(GetXmlCommentsPath());                    })                .EnableSwaggerUi(c =>                    {                                            });        }        //添加XML解析        private static string GetXmlCommentsPath()        {            return string.Format("{0}/bin/TestSwagger.XML", System.AppDomain.CurrentDomain.BaseDirectory);        }    }}

 注意修改相應的XML名字。

 來源:https://www.cnblogs.com/alunchen/p/6888002.html

【轉】C# 在webapi項目中配置Swagger

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.