Swagger在WebApi中的使用

1.打開NuGet管理器搜索並安裝 Swashbuckle和Swagger.Net兩項


Swashbuckle在Github的地址:https://github.com/domaindrivendev/Swashbuckle

swagger官網:https://swagger.io/


2.修改SwaggerConfig文件

去除註釋

  c.IncludeXmlComments(GetXmlCommentsPath());
並添加方法

  private static string GetXmlCommentsPath()
        {
            return String.Format(@"{0}\bin\WebApiSwagger.XML", System.AppDomain.CurrentDomain.BaseDirectory);
        }


3.修改SwaggetNet文件

  public static void PostStart() 
        {
            var config = GlobalConfiguration.Configuration;

            config.Filters.Add(new SwaggerActionFilter());
            
            try
            {
                config.Services.Replace(typeof(IDocumentationProvider),
                    new XmlCommentDocumentationProvider(HttpContext.Current.Server.MapPath("~/bin/WebApiSwagger.XML")));
            }
            catch (FileNotFoundException)
            {
                throw new Exception("Please enable \"XML documentation file\" in project properties with default (bin\\IntelligentService.XML) value or edit value in App_Start\\SwaggerNet.cs");
            }
        }



4.修改生成設置


5.啓動項目打開地址即可看到結果

http://localhost:1254/swagger/ui/index


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章