我目前已經設置了我的解決方案來爲每個端點生成Swagger文檔。不過,我有幾個端點只能用於管理員。在你的下方你將能夠看到一個例子。Swagger爲管理員和普通用戶生成不同的文檔
一個普通用戶可以創建模型,但是隻有管理員可以拉動數據庫中的每個模型。
面臨的挑戰是生成2套swagger文檔?一個供普通用戶查看,另一個供Admin用戶查看。我知道,如果我將[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)
{
...
}