Asp.net mvc 5 自定义ActionFilterAttribute OnActionExecuting中的MVC5/API2 CreateErrorResponse
使用MVC4,我能够创建并注册一个全局操作过滤器,该过滤器将在操作执行之前检查模型状态,并在造成任何损坏之前返回序列化的Asp.net mvc 5 自定义ActionFilterAttribute OnActionExecuting中的MVC5/API2 CreateErrorResponse,asp.net-mvc-5,asp.net-web-api2,Asp.net Mvc 5,Asp.net Web Api2,使用MVC4,我能够创建并注册一个全局操作过滤器,该过滤器将在操作执行之前检查模型状态,并在造成任何损坏之前返回序列化的ModelState public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { if (!actionContext.ModelState.IsValid) { actionContext.Reques
ModelState
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
但是,使用MVC5时,我很难找到请求
,因此CreateErrorResponse
public override void OnActionExecuting(ActionExecutingContext nActionExecutingContext)
{
if (!nActionExecutingContext.Controller.ViewData.ModelState.IsValid)
{
nActionExecutingContext.Result = // Where is Request.CreateErrorResponse ?
}
}
我意识到我可以创建一个自定义响应类来分配给Result
,但是如果CreateErrorResponse
仍然可用,我宁愿使用内置的响应类
你知道我在MVC5/WebAPI2中相对于ActionExecutingContext
在哪里可以找到它吗?请看这篇文章