Vs2019 16.11.9
.net core 3.1
1.创建的项目类型
2.安装 nuget包
Install-Package Swashbuckle.AspNetCore -Version 5.6.3
Install-Package Swashbuckle.AspNetCore.SwaggerGen -Version 5.6.3
Install-Package Swashbuckle.AspNetCore.SwaggerUI -Version 5.6.3
3.配置 黄色的部分加入
Startup.cs
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.OpenApi.Models;
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
}
public void Configure(IapplicationBuilder app, IwebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//启用中间件服务生成Swagger作为JSON终结点
app.UseSwagger();
//启用中间件服务对swagger-ui,指定Swagger JSON终结点
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
4.修改 实例控制器错误
WeatherForecastController [Route("api/WeatherForecast")]
launchsettings.json "launchUrl": "api/WeatherForecast",