【问题标题】:Swashbuckle should use my controller action names as default summarySwashbuckle 应该使用我的控制器操作名称作为默认摘要
【发布时间】:2021-05-28 14:42:49
【问题描述】:

我正在使用 Swashbuckle 在 .NET 5 项目中生成我的 API 定义。
为了在我的文档中添加摘要和备注,我目前正在对我的一些操作发表评论,如下所示:

    /// <summary>
    /// CreateSite
    /// </summary>
    /// <remarks>
    /// Options:
    /// * Enterprise = 0,
    /// * Site = 1
    /// * Order = 2
    /// * Line = 3
    /// * Product = 4
    /// 
    /// </remarks>
    [HttpPost]
    [Route("sites")]
    public async Task<IActionResult> CreateSiteAsync([FromBody] SiteCreateRequest createRequest)
    { // My controller stuff }

这会生成一个很好的文档并且非常有用。
但是,我的“摘要”字段总是与我的控制器操作名称具有相同的值 - 我已经将 efford 放在了一个非常好的操作命名中:
您可以在上面看到摘要包含“CreateSite”,而我的控制器名称是“CreateSiteAsync”。
有没有办法自动化这个?
那么我可以在服务中设置一些选项以使用控制器名称作为 json 文件中使用的“默认”摘要选项吗?

那么我可以在所有简单的请求中避免这种繁琐的 cmets 而无需任何文档。

【问题讨论】:

    标签: swagger .net-5 swashbuckle swashbuckle.aspnetcore


    【解决方案1】:

    我还使用 Swashbuckle 并使用 Swagger 标签正确记录我的 API。附上我实际使用的例子。对于您的特定控制器名称标签,在我的示例中为[SwaggerOperation("In-Transit Shipments")]

            /// <summary>
            /// In-transit shipments 
            /// </summary>
            /// <remarks>
            /// Get in-transit shipments for a client
            /// </remarks>
            /// <returns></returns>
            [SwaggerTag("GroundTransportation")]
            [SwaggerOperation("In-Transit Shipments")]
            [SwaggerResponse(200, typeof(List<LoadSummaryDto>), Description = "OK")]
            [SwaggerResponse(400, typeof(ErrorMessageDto), Description = "Bad Request")]
            [SwaggerResponse(404, typeof(ErrorMessageDto), Description = "Not Found")]
            [SwaggerOperationProcessor(typeof(ReDocCodeSampleAppender), "Curl,CSharp,Java")]
            [HttpGet("ShipmentInformation/In-Transit")]
            [TraceAction(message: "Controller: Retrieving in-transit shipments for client", level: LogLevel.Information, externalErrorMessage: "In-transit shipments could not be found")]
            public async Task<IActionResult> GetClientInTransitShipments(uint? page = GroundTransportationConstants.DefaultPage, uint? pageSize = GroundTransportationConstants.DefaultPageSize)
            {
               // ... a bunch of api code :-)
            }
    

    【讨论】:

    • 好吧,在这种情况下,我必须为每个控制器添加一个额外的 [SwaggerTag("SameNameLikeMyControllerAction")] 标记......它看起来比
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 2022-08-22
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 2015-11-10
    相关资源
    最近更新 更多