在 ASP.NET MVC 專案中,我們有幾個端點方法,其中一些標記是為所有方法設定的,例如ProducesResponseType((int)HttpStatusCode.OK)標記。
有沒有辦法為類中的所有方法設定一些回應?
如您所見,四行代碼僅用于狀態代碼
/// <summary> A summary describing the endpoint</summary>
[HttpPost("RenameSomething/", Name = "Rename[controller]")]
[Produces("application/json")]
[ProducesResponseType((int)HttpStatusCode.OK),
ProducesResponseType((int)HttpStatusCode.NotFound),
ProducesResponseType((int)HttpStatusCode.BadRequest),
ProducesResponseType((int)HttpStatusCode.InternalServerError)]
public ActionResult RenameSomething()
uj5u.com熱心網友回復:
您可以ProducesResponseType在控制器類上應用此類屬性,因為此屬性可以應用于由其AttributeUsage.
[System.AttributeUsage(
System.AttributeTargets.Class |
System.AttributeTargets.Method,
AllowMultiple=true, Inherited=true
)]
public class ProducesResponseTypeAttribute : Attribute
[ProducesResponseType((int)HttpStatusCode.OK),
ProducesResponseType((int)HttpStatusCode.NotFound),
ProducesResponseType((int)HttpStatusCode.BadRequest),
ProducesResponseType((int)HttpStatusCode.InternalServerError)
]
public class MyController : Controller
{
}
為了更進一步,您可以采用基于約定的方法,但這看起來比您要求的要多。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/375795.html
標籤:C# asp.net-mvc 接口 昂首阔步
