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");
}
}
5.啓動項目打開地址即可看到結果
http://localhost:1254/swagger/ui/index