从nugut包中下载
下载成功后在“StartUp.cs”下进行配置
public void ConfigureServices(IServiceCollection services)
{
//发布服务
services.AddSwaggerGen(m => {
m.SwaggerDoc("1.0", new OpenApiInfo { Title = "My API", Version = "1.0" });
});
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//使用服务
app.UseSwagger();
app.UseSwaggerUI(m=>m.SwaggerEndpoint("/swagger/1.0/swagger.json", "My API 1.0"));//位置和上面的1.0对应上
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
效果如下:
想要让swagger变为默认页面则需要设置
想让接口上有文档说明
StartUp.cs的文件配置如下