[英]How to define controller descriptions in ASP.NET Core Swagger (Swashbuckle.AspNetCore)?
I am trying Swagger in ASP.NET Core WebApi project and everything works fine - except controller descriptions.我正在 ASP.NET Core WebApi 项目中尝试 Swagger,一切正常 - 除了控制器描述。
For instance, I have UredskoPoslovanjeController and description in Swagger UI is UredskoPoslovanje and I can not find a way to change it.例如,我有 UredskoPoslovanjeController 并且 Swagger UI 中的描述是 UredskoPoslovanje,我找不到更改它的方法。
Only solution I found is listed here However, I think this is in conflict with API versions since versioning uses exact same attribute [ApiExplorerSettings(GroupName="v2")]
此处仅列出我找到的解决方案 但是,我认为这与 API 版本冲突,因为版本控制使用完全相同的属性
[ApiExplorerSettings(GroupName="v2")]
Here is swagger.json for this part: UredskoPoslovanje part in swagger.json这是这部分的swagger.json : swagger.json 中的 UredskoPoslovanje 部分
And my controlle is defined like this:我的控件是这样定义的:
/// <summary>
/// Uredsko poslovanje API
/// </summary>
[Authorize]
[Route("api/[controller]")]
public class UredskoPoslovanjeController : Controller
{
private LinkDbContext ctx;
public UredskoPoslovanjeController(LinkDbContext ctx)
{
this.ctx = ctx;
}
/// <summary>
/// Vraća broj pismena za zadani OIB
/// </summary>
/// <param name="OIB">OIB korisnika za koji se traži broj pismena</param>
/// <returns>Vraća broj pronađenih pismena</returns>
/// <response code="200">Vraća broj pismena za traženi OIB</response>
/// <response code="400">OIB ne postoji</response>
/// <response code="401">Nemate pristup metodi (neispravna autorizacija)</response>
[HttpGet("BrojPismena/{oib}")]
public ActionResult<BrojPismenaModel> DajBrojPismena(string OIB)
{
if (string.IsNullOrWhiteSpace(OIB)) return BadRequest("OIB ne smije biti prazan");
else
{
var osoba = ctx.Osoba.FirstOrDefault(x => x.Oib == OIB);
if (osoba == null) return BadRequest($"Osoba s OIB-om '{OIB}' ne postoji!");
else
{
return Ok(new BrojPismenaModel() { OIB = OIB, BrojPismena = ctx.UpPismeno.Count() });
}
}
}
}
I would expect "Uredsko poslovanje API" as controller description, but that does not happen - swagger ui screenshot我希望“Uredsko poslovanje API”作为控制器描述,但这不会发生 - swagger ui screenshot
Any idea how to properly set controller description?知道如何正确设置控制器描述吗?
Thanks, Mario谢谢,马里奥
By default the comments for the controller are not included.默认情况下,不包括控制器的注释。 The IncludeXmlComments method has a version that takes a boolean to indicate whether the controller XML comments should be used.
IncludeXmlComments 方法有一个版本,它采用布尔值来指示是否应使用控制器 XML 注释。 The code below is from my Startup : ConfigureServices method.
下面的代码来自我的 Startup : ConfigureServices 方法。
Original:原来的:
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
New:新的:
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath, true); // <== Added the true here, to show the controller description
Per the documentation, you can annotate controller actions and models with XML comments.根据文档,您可以使用 XML 注释来注释控制器操作和模型。 Not the controller itself.
不是控制器本身。
https://github.com/domaindrivendev/Swashbuckle.AspNetCore#include-descriptions-from-xml-comments https://github.com/domaindrivendev/Swashbuckle.AspNetCore#include-descriptions-from-xml-comments
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.