废话不多讲
第一步 当然是要通过 NuGet 安装第三方插件 swagger
程序包管理器控制台,安装命令:Install-Package Swashbuckle.AspNetCore -Pre
第二步 在Startup 文件中添加配置:
1. ConfigureServices 方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddMvc().AddJsonOptions(options =>
options.SerializerSettings.ContractResolver =
new Newtonsoft.Json.Serialization.DefaultContractResolver()); services.AddSwaggerGen();
services.ConfigureSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "MsSystem API"
}); options.IncludeXmlComments(Path.Combine(".\\bin\\netcoreapp2.1\\", "ApiDemo.xml"));
options.DescribeAllEnumsAsStrings();
});
}
2. Configure 方法
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug(); if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
} app.UseHttpsRedirection();
//app.UseMvc();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
}); app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MsSystem API V1");
});
}
第三步 设置输出地址
第四步 修改配置文件
最后 Ctrl+F5 如果出现以下界面,说明配置成功。