.NET Core 2.1 Swashbuckle - group controllers by area

不问归期 提交于 2021-02-09 20:30:12

问题


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}.

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!

UPDATE

I've tried annotating actions with tags.

This gives me:

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

What i want:

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

Update 2

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


回答1:


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" }
)]

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

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



来源:https://stackoverflow.com/questions/53505197/net-core-2-1-swashbuckle-group-controllers-by-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!