有没有办法在 swagger-ui 页面中更改控制器的名称? [英] Is there a way change the Controller's name in the swagger-ui page?

查看:31
本文介绍了有没有办法在 swagger-ui 页面中更改控制器的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Swashbuckle 在我的 WebApi 项目中启用 swagger 和 swagger-ui.

I'm using Swashbuckle to enable the use of swagger and swagger-ui in my WebApi project.

在下图中,您可以看到我的两个控制器显示在 swagger-ui 页面中.这些名称与 C# 代码中的名称相同,但我想知道是否有办法更改此处显示的内容?

In the following image you can see two of my controllers shown in the swagger-ui page. These are named as they are in the C# code, however I was wondering if there was a way to change what is shown here?

这主要是因为如您所见ManagementDashboardWidget 不是用户友好的名称,所以我想将其更改为用户友好.

This is mainly because as you can see ManagementDashboardWidget is not a user friendly name, so I want to change it to be user friendly.

推荐答案

您可以为此使用标签.默认情况下,Swashbuckle 会为每个操作添加一个带有控制器名称的标签.您可以使用 SwaggerOperationAttribute 覆盖它.例如,下一行将默认标签 Values 替换为标签 Test:

You can use tags for that. By default Swashbuckle adds a tag with the name of the controller to every operation. You can override that with the SwaggerOperationAttribute. For instance, the next line replaces the default tag, Values, with the tag Test:

public class ValuesController : ApiController
{
    [SwaggerOperation(Tags = new[] { "Test" })]
    public IHttpActionResult Get()
    {
        // ...
    }
}

Get 操作现在将放在 Test 组中.

The Getoperation will now be put in the group Test.

如果您希望操作出现在多个组中,您可以添加更多标签.例如:

If you want the operation to appear in multiple groups you can add more tags. For instance:

[SwaggerOperation(Tags = new[] { "Test", "Release1" })]

将把 Get 操作放在 TestRelease1 组中.

will put the Get operation in the groups Test and Release1.

这篇关于有没有办法在 swagger-ui 页面中更改控制器的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆