net swagger接口
引用NuGet包
Install-Package Swashbuckle.AspNetCore //控制台
Microsoft.Extensions.PlatformAbstractions //包管理,用来获取xml根目录
Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSwaggerGen(c =>
{
//var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
//var xmlPath = Path.Combine(basePath, "swaggerApi.xml");
//c.IncludeXmlComments(xmlPath);
c.SwaggerDoc("v1", new Info
{ Version = "v1",
Title = "测试接口",
Description = "一个测试接口",
TermsOfService = "None",
Contact = new Contact
{
Name = "Shayne Boyer",
Email = string.Empty,
Url = "https://twitter.com/spboyer"
},
License = new License
{
Name = "Use under LICX",
Url = "https://example.com/license"
}
});
});
services.ConfigureSwaggerGen(c =>
{
c.IncludeXmlComments(GetXmlSwaggerCommentsFilePath());
});
} private string GetXmlSwaggerCommentsFilePath()
{
var app = PlatformServices.Default.Application;
return Path.Combine(app.ApplicationBasePath, Path.ChangeExtension(app.ApplicationName, "xml"));
}
csproj
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);</NoWarn>
</PropertyGroup> <PropertyGroup>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
<NoWarn>;;;</NoWarn>
</PropertyGroup>
修改项目属性