英文:
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 =>
{
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);
});
Then, in my MainController.cs I do the following:
[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();
}
}
And in my SchoolController.cs I do the following:
[Route("api/school")]
[Tags("School Endpoints")]
[ApiExplorerSettings(GroupName = "school")]
[ApiController]
public class MainController : ControllerBase
{
[HttpGet]
[Route("hello-world")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<string> Alive()
{
return "Hello World!";
}
}
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");
});
结果:
你可以阅读 Swashbuckle.AspNetCore 以了解更多信息。
英文:
Try to remove below code from your startup:
d.DocInclusionPredicate((name, api) => true);
Besides, remove this line from your main controller Alive():
[ApiExplorerSettings(GroupName = "v1")]
In startup, I specify one or more Swagger endpoints like :
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/main/swagger.json", "main");
c.SwaggerEndpoint("/swagger/school/swagger.json", "school");
});
result:
You can read Swashbuckle.AspNetCore to know more.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论