.NET Core 2.1 Swashbuckle组控制器(按区域) [英] .NET Core 2.1 Swashbuckle - group controllers by area

查看:47
本文介绍了.NET Core 2.1 Swashbuckle组控制器(按区域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况很简单.我有一个非常大的.NET Core 2.1 MVC/WebApi,分为几个区域,分别代表系统的不同模块.我使用Swagger(SwashBuckle),效果很好.我的路由类似于 {area}/{controller}/{action} .

My situation is rather simple. I have a very large .NET Core 2.1 MVC/WebApi divided into several Areas, representing different modules of my system. I use Swagger (SwashBuckle) and it works very well. My routing are like {area}/{controller}/{action}.

在Swagger UI中,每个动作都被分组到控制器中(标准行为).我的控制器和操作列表变得非常庞大且难以掌握.因此,我很想Swagger可以将我的控制器划分到不同的区域!可以折叠 area x area x 中的每个控制器.

In Swagger UI, every action is grouped into the controllers (standard behaviour). My list of controllers and operations is becoming very very large and hard to grasp. Because of that, i would love if Swagger could divide my controllers into the different areas! Making it possible to collapse area x and every controller within area x.

我真的很想念此功能或自己实现的方法!任何想法表示赞赏!

I really miss this feature or a way to implement it myself! Any ideas are appreciated!

更新

我尝试用标签注释动作.

I've tried annotating actions with tags.

这给了我

- Area 1
    - MethodFromControllerA()
    - MethodFromControllerB()
- Area 2
    - MethodFromControllerC()
    - MethodFromControllerD()

我想要什么:

- Area 1
    - ControllerA
        - MethodFromControllerA()
    - ControllerB
        - MethodFromControllerB()
- Area 2
    - ControllerC
        - MethodFromControllerC()
    - ControllerD
        - MethodFromControllerD()

更新2

另一种选择是针对我的每个领域制定几个规范.就像每个区域的不同Swagger UI一样.有可能吗?

Another option would be to have several specifications for each of my areas. Like different Swagger UIs for every area. Possible?

推荐答案

您首先需要安装注释并在启动时启用它们:

You first need to install annotations and enable them in your startup:

services.AddSwaggerGen(c =>
{
   c.EnableAnnotations();
});

然后,您需要将区域"作为标签添加到每个操作中.

Then you need to add your "area" as tag to every action.

[SwaggerOperation(
    Tags = new[] { "Area51" }
)]

当您打开swagger ui时,应该立即按标签将其自动分组(默认情况下,控制器名称为所选标签).

When you open your swagger ui, it should be automatically grouped by tag now (per default the controller name is the chosen tag).

当前无法使用现有的swagger ui生成器对端点进行进一步的嵌套分组.

Further nested grouping of endpoints is currently not possible out of the box with the existing swagger ui generator.

这篇关于.NET Core 2.1 Swashbuckle组控制器(按区域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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