C#(.Net Core WebAPI)之API文档的生成(Swagger)
2021-02-02 12:13
标签:developer tor rect scribe conf call tom set name 搜Swashbuckle.AspNetCore Ⅰ : Startup.cs ②:Configure中 ①,到处项目XML , 加入1591禁止警告 结果: C#(.Net Core WebAPI)之API文档的生成(Swagger) 标签:developer tor rect scribe conf call tom set name 原文地址:https://blog.51cto.com/aonaufly/2439347
在NuGet 中,安装 Swashbuckle.AspNetCore :
我使用的版本为 : 5.0.0-rc2二 : 引入Swagger功能
① ,ConfigureServices方法中:public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options =>
{
options.SerializerSettings.Formatting = Formatting.Indented;
});
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo()
{
Title = "Swagger Test UI",
Version = "v1",
Description = "Aonaufly first ASP.NET Core Web API"
});
options.CustomSchemaIds(type => type.FullName); // 解决相同类名会报错的问题
options.IncludeXmlComments(Path.Combine(Directory.GetCurrentDirectory(), "WebAPIPoco.xml")); // 标注要使用的 XML 文档
options.DescribeAllEnumsAsStrings();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//设置全局跨域
app.UseCors(builder => builder.AllowAnyOrigin());
app.UseHttpsRedirection();
app.UseSwagger(c => { c.RouteTemplate = "swagger/{documentName}/swagger.json"; });
// 在这里面可以注入
app.UseSwaggerUI(options =>
{
options.ShowExtensions();
options.ValidatorUrl(null);
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Aonaufly API V1");
options.DocExpansion(DocExpansion.None);
});
app.UseMvc();
}
三 :配置设置
②,将项目XML生成路径复制到项目根路径
copy $(TargetDir)WebAPIPoco.xml $(ProjectDir)WebAPIPoco.xml
③,重置默认网页为swagger , 默认是 api/values四 :初始结果
五 : 测试
///
/// 输入 : int
/// 输出 : string
///
///
上一篇:win10安装docker步骤
下一篇:c#模拟Http请求
文章标题:C#(.Net Core WebAPI)之API文档的生成(Swagger)
文章链接:http://soscw.com/index.php/essay/49953.html