![](/img/trans.png)
[英]Swashbuckle fails in asp.net core with NotSupportedException exception
[英]ASP.NET Core error handler without HTTP verb throws Swashbuckle exception
在錯誤處理的文檔中有這樣的:
警告
不要使用 HTTP 方法屬性標記錯誤處理程序操作方法,例如 HttpGet。 顯式動詞會阻止某些請求到達操作方法。
所以它推薦這個:
[ApiController]
public class ErrorController : ControllerBase
{
//[HttpGet] <-- docs say not to do this
[Route("/error")]
public IActionResult Error() => Problem();
}
但是,當我遵循該建議(而不是[HttpGet("/error")]
)時,我從 Swashbuckle 收到此錯誤:
Swashbuckle.AspNetCore.SwaggerGen.SwaggerGeneratorException:操作的 HTTP 方法不明確 - ErrorController.Error。 操作需要 Swagger/OpenAPI 3.0 的顯式 HttpMethod 綁定
顯然,當我包含動詞時,它會起作用。
有人可以在文檔中解釋該建議背后的原因嗎?
文檔中建議的原因是允許任何類型的 HTTP 請求在發生錯誤時到達錯誤處理程序。 例如,如果您將其標記為[HttpGet]
,它將無法處理 POST 請求中的錯誤。
為了[ApiExplorerSettings(IgnoreApi = true)]
與 Swagger 配合使用,您可以使用以下屬性標記控制器(甚至特定操作): [ApiExplorerSettings(IgnoreApi = true)]
這將導致 Swashbuckle 不為其生成元數據,也不會在 Swagger UI 中顯示它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.