在錯誤處理的檔案中有這樣的:
警告
不要使用 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 系結
顯然,當我包含動詞時,它會起作用。
有人可以在檔案中解釋該建議背后的原因嗎?
uj5u.com熱心網友回復:
檔案中建議的原因是允許任何型別的 HTTP 請求在發生錯誤時到達錯誤處理程式。例如,如果將其標記為[HttpGet],它將無法處理 POST 請求中的錯誤。
為了讓它與 Swagger 配合使用,您可以使用此屬性標記控制器(甚至特定操作):
[ApiExplorerSettings(IgnoreApi = true)]
這將導致 Swashbuckle 不為其生成元資料,也不會在 Swagger UI 中顯示它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/317595.html
標籤:C# asp.net核心 路线 花环 asp.net-core-5.0
