简体   繁体   English

如何在 ASP.NET Core Swagger (Swashbuckle.AspNetCore) 中定义控制器描述?

[英]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.jsonswagger.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.

相关问题 swashbuckle.aspnetcore 是否支持 asp.net core 3.0? - Is swashbuckle.aspnetcore supporting asp.net core 3.0? asp.net core swashbuckle 如何在同一个控制器中使用版本控制 - asp.net core swashbuckle how to use versioning in same controller 迁移到 Swashbuckle.AspNetCore 版本 5 时,Swagger UI 中的不记名身份验证 - Bearer authentication in Swagger UI, when migrating to Swashbuckle.AspNetCore version 5 ASP.NET Core - Swashbuckle 未创建 swagger.json 文件 - ASP.NET Core - Swashbuckle not creating swagger.json file 使用 ASP.NET Core Web API 重命名 Swashbuckle 6 (Swagger) 中的模型 - Rename model in Swashbuckle 6 (Swagger) with ASP.NET Core Web API Swashbuckle/Swagger + ASP.Net Core:“无法加载 API 定义” - Swashbuckle/Swagger + ASP.Net Core: "Failed to load API definition" 如何使用 Swashbuckle.AspNetCore 在 Swagger 模式中将自定义泛型类型公开为字符串 - How to expose a custom generic type as a string in Swagger schema using Swashbuckle.AspNetCore 数组类型<example>标签不适用于 Swagger (swashbuckle.aspnetcore)</example> - Array types with <example> tag not working with Swagger (swashbuckle.aspnetcore) 如何使用Swashbuckle.AspNetCore隐藏响应代码200? - How can I hide response code 200 with Swashbuckle.AspNetCore? Swashbuckle + ASP.Net Core WebApi:Swagger 文档不包含用于版本选择的 Request-Header 或 QueryParameter? - Swashbuckle + ASP.Net Core WebApi: Swagger Document does not include Request-Header or QueryParameter for version selection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM