- 直接导入一个包: Install-Package Swashbuckle
- 然后F5启动项目 输入 地址:你的地址/swagger
安装完成后,写注释并没有在swagger页面上面增加,所以我们现在开开启注释
项目属性中勾选 XML文档文件
在SwaggerConfig类中,EnableSwagger的时候添加下面XML解析
c.IncludeXmlComments(GetXmlCommentsPath());
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());
c.UseFullTypeNameInSchemaIds();
c.DescribeAllEnumsAsStrings();
})
.EnableSwaggerUi(c =>
{
});
}
//添加XML解析
private static string GetXmlCommentsPath()
{
//注意修改相应的XML名字。
return string.Format("{0}/bin/TestSwagger.XML", System.AppDomain.CurrentDomain.BaseDirectory);
}
}
}