首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swagger (Asp.Net核心)有控制器描述吗?

Swagger (Asp.Net核心)有控制器描述吗?
EN

Stack Overflow用户
提问于 2016-11-01 22:58:26
回答 7查看 25.9K关注 0票数 16

我正在构建一个REST服务,它将承载多个控制器(微服务)。作为一个整体,让我们称之为“鲍勃”服务。因此,swagger会显示" Bob“/”Bob Microservices的集合“。然后列出控制器名称。现在,它只显示了XYZ,ABC等。有没有办法让"XYZ -一个XYZ的集合“或者诸如此类的东西摆出来呢?

看起来像swagger在方法上显示/汇总,而不是在控制器上显示。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2016-11-02 01:30:41

有没有办法让大家大摇大摆地展示"XYZ - XYZ的集合“

是。这是最简单的方法之一。Swagger的ASP.NET核心版本利用了ApiExplorerSettings属性。您可以设置GroupName

代码语言:javascript
运行
复制
public class BobController 
{
    [ApiExplorerSettings(GroupName="XYZ - A collection of XYZ APIs")]
    public IActionResult MyAction() 
    {
        ...
    }
}

组名称出现在Swagger中,其中组的操作列在下面的操作中。

编辑:是一个基于大锤评论的想法。

Swagger ASP.NET Core使用一个IApiDescriptionGroupCollectionProvider来构建它的描述组。我们可以实现自己的,使用默认的ApiDescriptionGroupCollectionProvider作为灵感,并在Startup.ConfigureServices期间注册提供者。我们的实现将使ApiDescriptionGroups()方法返回与每个操作的控制器相关联的GroupName。然后,我们可以将ApiExplorerSettings属性放在每个控制器上,而不是放在每个操作上。

票数 7
EN

Stack Overflow用户

发布于 2019-05-31 13:14:48

如果您使用的是SwashBackle4.0.x和ASP.NET Core2.x,您可能有类似的东西,它也可以通过包含用于ASP.NET的NuGet包来工作。

代码语言:javascript
运行
复制
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Swashbuckle.AspNetCore.Annotations;

namespace MyExample.Controllers
{
/// <summary>
/// Example of a .NET Core controller based on the controller name
/// api/[controller] on ValuesController becomes api/values
/// endpoint: "/Values" from [controller] and name of controller , which is "ValuesController"
/// </summary>
[Route("[controller]")]
[ApiController]
[SwaggerTag("This is an example controller generated by ASP.NET Core 2.x")]
public class ValuesController : ControllerBase
{
...
}

然后,我在Startup.cs方法中的ConfigureServices昂首阔步代码如下所示(编辑后包括来自Iain的贡献,以包含控制器头注释):

代码语言:javascript
运行
复制
services.AddSwaggerGen(c =>
{
    // Set Title and version
    c.SwaggerDoc("v1", new Info { Title = "My Example API", Version = "v1", Description = "The API for my application" });
    // Set the comments path for the Swagger JSON and UI.
    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
    // pick comments from classes, including controller summary comments
    c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true); 
    // _OR_ enable the annotations on Controller classes [SwaggerTag], if no class comments present
    c.EnableAnnotations();
});

然后我的控制器就会被装饰

票数 17
EN

Stack Overflow用户

发布于 2019-11-14 01:23:27

我正在寻找类似的答案,并希望能够使用控制器类上的摘要XML注释来提供控制器描述。结果表明,您可以通过在启动时的Swagger配置中添加includeControllerXmlComments: true来做到这一点:

代码语言:javascript
运行
复制
    services.AddSwaggerGen(c =>
    {
        // Set the comments path for the Swagger JSON and UI.
        var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
    });

因此:

代码语言:javascript
运行
复制
    /// <summary>
    /// My controller description
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]

显示为:

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40369914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档