搜Swashbuckle.AspNetCore
在NuGet 中,安裝 Swashbuckle.AspNetCore :
我使用的版本為 : 5.0.0-rc2
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供南和網(wǎng)站建設、南和做網(wǎng)站、南和網(wǎng)站設計、南和網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、南和企業(yè)網(wǎng)站模板建站服務,十余年南和做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
Ⅰ : Startup.cs
① ,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();
});
}
②:Configure中
// 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 , 加入1591禁止警告
②,將項目XML生成路徑復制到項目根路徑
copy $(TargetDir)WebAPIPoco.xml $(ProjectDir)WebAPIPoco.xml
③,重置默認網(wǎng)頁為swagger , 默認是 api/values
///
/// 帶參數(shù)的get請求
///
///
///
/// 輸入 : int
/// 輸出 : string
///
///
/// ID號
/// String
/// 返回字符串
/// 如果id為空
// GET api/values/5
[HttpGet("{id}")]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
public ActionResult Get(int id)
{
return "value";
}
結果: