如何将两个控制器拆分为两个Swagger定义

huangapple go评论65阅读模式
英文:

How do I split two controllers to two Swagger definitions

问题

在我的ASP.NET Web API项目中,我正在创建一个相当大的API。我希望有多个定义,以确保类别正确分割,并使用定义内的标签来实现。

在我的Startup文件中,我执行以下操作:

services.AddSwaggerGen(d =>
{
    d.SwaggerDoc("main", new OpenApiInfo
    {
        Title = "Main",
        Version = "v1",
        Description = "The main information",
        Contact = new OpenApiContact
        {
            Name = "itsfinniii"
        }
    });

    d.SwaggerDoc("school", new OpenApiInfo
    {
        Title = "School",
        Version = "v1",
        Description = "School stuff",
        Contact = new OpenApiContact
        {
            Name = "itsfinniii"
        }
    });

    d.DocInclusionPredicate((name, api) => true);
});

然后,在我的MainController.cs文件中,我执行以下操作:

[Route("api")]
[Tags("Main Endpoints")]
[ApiExplorerSettings(GroupName = "main")]
[ApiController]
public class MainController : ControllerBase
{
    [HttpGet]
    [Route("alive")]
    [ApiExplorerSettings(GroupName = "v1")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    public async Task<IActionResult> Alive()
    {
        return new NoContentResult();
    }
}

在我的SchoolController.cs文件中,我执行以下操作:

[Route("api/school")]
[Tags("School Endpoints")]
[ApiExplorerSettings(GroupName = "school")]
[ApiController]
public class SchoolController : ControllerBase
{
    [HttpGet]
    [Route("hello-world")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<string> HelloWorld()
    {
        return "Hello World!";
    }
}

然而,现在两个标签和两个终端点都出现在两个定义中。我应该如何使只属于"main"的终端点仅出现在"main"定义中,而只属于"school"的终端点仅出现在"school"定义中?

英文:

So, I am making a pretty big API in an ASP.NET Web API project. I wanted to have multiple definitions to make sure categoriers are split correctly, with the use of tags within the definitions.

In my Startup file, I do the following:

services.AddSwaggerGen(d =&gt;
        {
            d.SwaggerDoc(&quot;main&quot;, new OpenApiInfo
            {
                Title = &quot;Main&quot;,
                Version = &quot;v1&quot;,
                Description = &quot;The main information&quot;,
                Contact = new OpenApiContact
                {
                    Name = &quot;itsfinniii&quot;
                }
            });

            d.SwaggerDoc(&quot;school&quot;, new OpenApiInfo
            {
                Title = &quot;School&quot;,
                Version = &quot;v1&quot;,
                Description = &quot;School stuff&quot;,
                Contact = new OpenApiContact
                {
                    Name = &quot;itsfinniii&quot;
                }
            });

            d.DocInclusionPredicate((name, api) =&gt; true);
        });

Then, in my MainController.cs I do the following:

    [Route(&quot;api&quot;)]
    [Tags(&quot;Main Endpoints&quot;)]
    [ApiExplorerSettings(GroupName = &quot;main&quot;)]
    [ApiController]
    public class MainController : ControllerBase
    {
        [HttpGet]
        [Route(&quot;alive&quot;)]
        [ApiExplorerSettings(GroupName = &quot;v1&quot;)]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        public async Task&lt;IActionResult&gt; Alive()
        {
            return new NoContentResult();
        }
    }

And in my SchoolController.cs I do the following:

    [Route(&quot;api/school&quot;)]
    [Tags(&quot;School Endpoints&quot;)]
    [ApiExplorerSettings(GroupName = &quot;school&quot;)]
    [ApiController]
    public class MainController : ControllerBase
    {
        [HttpGet]
        [Route(&quot;hello-world&quot;)]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task&lt;string&gt; Alive()
        {
            return &quot;Hello World!&quot;;
        }
    }

However, both tags and both endpoints appear within both definitions. How could I make it so that only those that belong to main appear in the main definition, and nowhere else, and that those that belong to school only appear in the school definition?

答案1

得分: 0

尝试从启动代码中删除以下部分:

d.DocInclusionPredicate((name, api) => true);

此外,从主控制器的 Alive() 方法中删除以下行:

[ApiExplorerSettings(GroupName = "v1")]

在启动中,我指定了一个或多个Swagger端点,如下:

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/main/swagger.json", "main");
    c.SwaggerEndpoint("/swagger/school/swagger.json", "school");
});

结果:

如何将两个控制器拆分为两个Swagger定义

你可以阅读 Swashbuckle.AspNetCore 以了解更多信息。

英文:

Try to remove below code from your startup:

d.DocInclusionPredicate((name, api) =&gt; true);

Besides, remove this line from your main controller Alive():

[ApiExplorerSettings(GroupName = &quot;v1&quot;)]

In startup, I specify one or more Swagger endpoints like :

 app.UseSwaggerUI(c =&gt;
                {
                    c.SwaggerEndpoint(&quot;/swagger/main/swagger.json&quot;, &quot;main&quot;);
                    c.SwaggerEndpoint(&quot;/swagger/school/swagger.json&quot;, &quot;school&quot;);
                });

result:

如何将两个控制器拆分为两个Swagger定义

You can read Swashbuckle.AspNetCore to know more.

huangapple
  • 本文由 发表于 2023年4月4日 04:22:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定