Web API中的模型验证-在没有throw语句的情况下抛出异常? [英] Model validation in Web API - Exception is thrown with out a throw statement?

查看:48
本文介绍了Web API中的模型验证-在没有throw语句的情况下抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从该代码段如下所示,在 Web API

The code-snippet is as below in Web API

public class ValidateModel : ActionFilterAttribute
    {
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            if (actionContext.ModelState.IsValid == false)
            {
                actionContext.Response = actionContext.Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest, actionContext.ModelState);
            }

            base.OnActionExecuting(actionContext);
        }
    }

问题是在验证模型后,如果有任何错误,它会分配模型状态无效异常.

The problem is upon validation of model, if there were any errors, it assigns an model state invalid exception.

然后,在转到实际方法(用此 [ValidateModel] 属性修饰)之前,WebAPI只需返回400请求.

And after that, before going to the actual method (which is decorated with this [ValidateModel] attribute), WebAPI simply returns a 400 request.

但是如何?哪个函数返回 HTTP 400 ?

But how ? Which function is returning a HTTP 400?

执行此方法后会发生什么?控制流向何处?

What happens after this method is done executing? Where does the control flow ?

我将此属性应用于的操作是正常操作.

The action that I am applying this attribute to is a normal one.

[ValidateModel]
public IHttpActionResult Post([FromBody]Request)
{
//do normal business logics here.
return Ok(SuccessMessage);
}

推荐答案

要了解控制流,您首先需要在此处访问此链接-

To understand the control flow you first need to visit this link here -

操作属性类参考

检查那里的方法部分.如果明确指出,则在执行以该属性装饰的特定动作之前执行 OnActionExecuting ,并在执行完成之后执行 OnActionExecuted .由于您正在像这样实现 OnActionExecuting -

Check the method sections there. If clearly states that, OnActionExecuting is performed before that particular action, decorated with this attribute, is executed and OnActionExecuted is performed after the execution is complete. Since you are implementing OnActionExecuting like this -

public class ValidateModel : ActionFilterAttribute
{
    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        if (actionContext.ModelState.IsValid == false)
        {
            actionContext.Response = actionContext.Request.CreateErrorResponse(
                HttpStatusCode.BadRequest, actionContext.ModelState);
        }

        base.OnActionExecuting(actionContext);
    }
}

并且由于在此方法中引发了错误,因此-

and since the error is thrown inside this method, like this -

 if (actionContext.ModelState.IsValid == false)
        {
            actionContext.Response = actionContext.Request.CreateErrorResponse(
                HttpStatusCode.BadRequest, actionContext.ModelState);
        }

仅当 OnActionExecuting 方法找到有效模型时,您的方法才会执行.从错误400来看,您当前的模型状态似乎无效,因此无法成功执行,并且除非提供了有效的模型,否则您的方法将永远不会执行.在此方法中设置一个调试点,您就可以找到失败的原因.

Your method will only execute if the OnActionExecuting method finds a valid model. And from the error 400, it seems that your current model state is not valid and thus it fails to succeed and your method will never be executed unless you provide a valid model. Set a debug point inside this method and you can find out why it fails.

顺便说一句,异常不是仅由-

By the way, the exception is not thrown its just a standard response that is handled by -

base.OnActionExecuting(actionContext);

http://msdn.microsoft.com/zh-CN/library/system.web.mvc.actionfilterattribute(v=vs.118).aspx

这篇关于Web API中的模型验证-在没有throw语句的情况下抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆