从 Action 方法参数的 ASP.NET MVC 3 默认模型绑定器强制 UpdateModel 行为

发布于 2024-12-29 00:15:18 字数 802 浏览 1 评论 0原文

我正在 ASP.Net MVC 3 应用程序中实现错误处理策略。我编写了一个实现 IExceptionFilter 等的属性。该属性可以正确运行,处理操作方法中引发的异常,并返回序列化为 JSON 的异常信息。

我想使用此属性来处理模型绑定程序在将数据传递到操作方法时发现的验证错误。例如,如果我将一个对象 POST 到一个被反序列化为 Action Method 参数的 Action Method,如果像 UpdateModel 那样发生验证错误,我希望它抛出异常。现在,默认模型绑定器的行为似乎与 TryUpdateModel 类似,只是翻转 ModelState.IsValid 而不是抛出异常。

[ActionExceptionJsonHandler]
public ActionResult CreateSomething(SomethingViewData account)
{
// If model binding fails validation an exception should be thrown and no code is executed here
// Do stuff here
}

如果默认模型绑定器以与 UpdateModel 相同的方式引发异常,则 IExceptionFilter 将捕获该异常并处理将验证错误返回给客户端。如果没有的话,开发人员必须编写代码来检查 ModelState 等。

所以底线我有两个相关的问题:

  1. 有没有办法让默认模型绑定器在验证失败时抛出异常?
  2. 对于使用这种方法而不是在每个操作方法中手动检查 ModelState 有什么想法吗?

谢谢。

I am implementing an error handling strategy in an ASP.Net MVC 3 application. I've written an attribute implementing IExceptionFilter etc. This functions correctly, handling exceptions throw in the action method and returns the exception information serialized as JSON.

I would like to use this attribute to also handle validation errors found by the model binder when passing data to an Action Method. For example, if I POST an object to an Action Method which is deserialized as the Action Method parameter I would like it to throw an exception if a validation error occurs like UpdateModel does. Right now the default model binder seems to behave like TryUpdateModel, simply flipping the ModelState.IsValid rather than throwing an exception.

[ActionExceptionJsonHandler]
public ActionResult CreateSomething(SomethingViewData account)
{
// If model binding fails validation an exception should be thrown and no code is executed here
// Do stuff here
}

If the default model binder threw an exception in the same way as UpdateModel does then the IExceptionFilter would catch it and handle returning the validation error to the client. Without that the developer must write code to check the ModelState etc.

So bottom line I have two related questions:

  1. Is there a way to have the default model binder throw an exception when validation fails?
  2. Any thoughts on using this approach versus manually checking ModelState in each action method?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2025-01-05 00:15:18

我的解决方案最终是实现一个 ActionFilterAttribute ,如下所示。在 OnActionExecuting 中,我检查 ModelState.IsValid,如果为 false,我将模型状态错误序列化为 JSON 并设置 Result 对象,有效取消执行。这允许我返回包含模型绑定错误的自定义 JSON 序列化对象。

    public override void OnActionExecuting(ActionExecutingContext filterContext) {

        if (filterContext.Controller.ViewData.ModelState.IsValid) {
            base.OnActionExecuting(filterContext);
            return;
        }

        var returnDto = new ReturnDto
                            {
                                Success = false,
                                Errors = Tools.GetModelStateErrors(filterContext.Controller.ViewData.ModelState)
                            };

        // AllowGet is fine provided we are not returning a javascript array
        filterContext.Result = new JsonResult { Data = returnDto, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }

My solution ended up being to implement an ActionFilterAttribute as shown below. In OnActionExecuting I check ModelState.IsValid, if it's false I serialize the model state errors as JSON and set the Result object effectively canceling execution. This allows for me to return a custom JSON serialized object containing the model binding errors.

    public override void OnActionExecuting(ActionExecutingContext filterContext) {

        if (filterContext.Controller.ViewData.ModelState.IsValid) {
            base.OnActionExecuting(filterContext);
            return;
        }

        var returnDto = new ReturnDto
                            {
                                Success = false,
                                Errors = Tools.GetModelStateErrors(filterContext.Controller.ViewData.ModelState)
                            };

        // AllowGet is fine provided we are not returning a javascript array
        filterContext.Result = new JsonResult { Data = returnDto, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文