C# Swagger为管理员和普通用户生成不同的文档

C# Swagger为管理员和普通用户生成不同的文档,c#,swagger,C#,Swagger,目前,我的解决方案已设置为为每个端点生成Swagger文档。但是,我有几个端点,只对管理员可用。下面你可以看到一个例子 普通用户可以创建模型,但是只有管理员可以提取数据库中的每个模型 挑战是生成两套招摇过市的文档?一个供普通用户查看,另一个供管理员用户查看。我知道,如果我将[ApiExplorerSettings(IgnoreApi=true)]添加到我的端点,它将不会出现在生成的文档中,但这意味着我的管理员用户也将无法看到重要的文档。任何关于如何根据用户动态生成两组文档的建议都会有所帮助 [S

目前,我的解决方案已设置为为每个端点生成Swagger文档。但是,我有几个端点,只对管理员可用。下面你可以看到一个例子

普通用户可以创建模型,但是只有管理员可以提取数据库中的每个模型

挑战是生成两套招摇过市的文档?一个供普通用户查看,另一个供管理员用户查看。我知道,如果我将[ApiExplorerSettings(IgnoreApi=true)]添加到我的端点,它将不会出现在生成的文档中,但这意味着我的管理员用户也将无法看到重要的文档。任何关于如何根据用户动态生成两组文档的建议都会有所帮助

[SwaggerResponse((int)System.Net.HttpStatusCode.OK, Type = typeof(RestOkResponse<PackageResponse>))]
[SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.Forbidden, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)]
[HttpPost("/v1/packages")]
[Authorize()]
public async Task<IActionResult> CreateModel([FromBody]Request request)
{
    ...
}
[SwaggerResponse((int)System.Net.HttpStatusCode.OK,Type=typeof(RestOkResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError,Type=typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest,Type=typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.probled,Type=typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)]
[HttpPost(“/v1/packages”)]
[授权()]
公共异步任务CreateModel([FromBody]请求)
{
...
}
以下方法仅适用于管理员:

[SwaggerResponse((int)System.Net.HttpStatusCode.OK, Type = typeof(RestOkResponse<PackageResponse>))]
[SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.Forbidden, Type = typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)]
[ApiExplorerSettings(IgnoreApi = true)]
[HttpPost("/v1/packages")]
[Authorize()]
public async Task<IActionResult> GetAllModelsFromDatabase([FromBody]Request request)
{
    ...
}
[SwaggerResponse((int)System.Net.HttpStatusCode.OK,Type=typeof(RestOkResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError,Type=typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest,Type=typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.probled,Type=typeof(RestErrorResponse))]
[SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)]
[ApiExplorerSettings(IgnoreApi=true)]
[HttpPost(“/v1/packages”)]
[授权()]
公共异步任务GetAllModelsFromDatabase([FromBody]请求)
{
...
}

在这个答案中发现了一个动态过程

可以分离swagger文档,但是没有内置的方法。必须从一个文档文件中删除不需要的节点:

如果您自己主持该编辑器,那么在当前编辑器中这可以很好地工作 参数\u common.yaml路径可以解析为HTTP路径。 目前无法在文件之间跳转或创建新文件。 如果你正在大摇大摆地做一个大项目,我建议你主持 编辑自己。当编辑器和您正在构建的API位于 同一来源,XHR调用不必是跨来源的,这有助于编辑器 在“try operation”和API中显示有关调用的更多详细信息 不必有跨原点的标题

有关如何将swagger文件拆分为较小节点的示例。