【发布时间】:2017-06-21 22:15:01
【问题描述】:
我目前已将我的解决方案设置为为每个端点生成 Swagger 文档。但是,我有几个仅适用于管理员的端点。在下面,您将能够看到一个示例。
普通用户可以创建模型,但只有管理员可以提取数据库中的每个模型。
挑战是生成 2 套 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<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)
{
...
}
【问题讨论】: