官档地址:
个人整理:
1、开启项目文档:属性-生成-XML文档文件,debug和release都要配置
成功后.csproj文件中会出现
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>ProjectName.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile>ProjectName.xml</DocumentationFile>
</PropertyGroup>
2、Nuget安装 Swashbuckle.AspNetCore
3、Startup.ConfigureServices中添加
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("doc", new Info() { Title = "@title" });
options.IncludeXmlComments("ProjectName.xml");
});
4、Startup.Configure中添加
app.UseSwagger();
app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/doc/swagger.json", null));
5、访问地址:/swagger/index.html
补充-如果还需要调试时启动,修改launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5857/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ProjectName": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5858/"
}
}
}