您好,登錄后才能下訂單哦!
搜Swashbuckle.AspNetCore
在NuGet 中,安裝 Swashbuckle.AspNetCore :
我使用的版本為 : 5.0.0-rc2
Ⅰ : 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
③,重置默認網頁為swagger , 默認是 api/values
/// <summary>
/// 帶參數的get請求
/// </summary>
/// <remarks>
/// <code>
/// 輸入 : int
/// 輸出 : string
/// </code>
/// </remarks>
/// <param name="id">ID號</param>
/// <returns>String</returns>
/// <response code="201">返回字符串</response>
/// <response code="400">如果id為空</response>
// GET api/values/5
[HttpGet("{id}")]
[ProducesResponseType(201)]
[ProducesResponseType(400)]
public ActionResult<string> Get(int id)
{
return "value";
}
結果:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。