1.设置项目生成xml:项目 / 右键 / 属性 / 生成 / 勾选复选框[XML文档文件]
2.添加Nuget:Swash
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "WebApi");
// 添加方法和实体注释
{
var path = $@"{System.AppDomain.CurrentDomain.BaseDirectory}\bin\Webapi.xml";
if (File.Exists(path)) c.IncludeXmlComments(path);
}
{
var path = $@"{System.AppDomain.CurrentDomain.BaseDirectory}\bin\Models.xml";
if (File.Exists(path)) c.IncludeXmlComments(path);
}
// 添加控制器注释
var xmlFile = $@"{System.AppDomain.CurrentDomain.BaseDirectory}\bin\Webapi.xml";
c.CustomProvider((defaultProvider) => new SwaggerControllerDescProvider(defaultProvider, xmlFile));
})
.EnableSwaggerUi(c =>
{
// 添加控制器注释: 项目名+路径+文件名,当前为项目根目录
c.InjectJavaScript(thisAssembly, "Webapi.CustSwagger.js");
});
}
}
buckle.Core,为SwaggerConfig.cs添加配置