Any way to recover model passed to POST action when inside OnException(ExceptionContext filterContext)?

后端 未结 1 1989
野趣味
野趣味 2021-01-12 20:38

Situation is this:

I can\'t find a way of getting the viewModel that was passed to the POST action method.

[HttpPost]
public ActionResul         


        
1条回答
  •  时光说笑
    2021-01-12 21:08

    You could create a custom model binder that inherits from DefaultModelBinder and assign the model to TempData:

    public class MyCustomerBinder : DefaultModelBinder
    {
        protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            base.OnModelUpdated(controllerContext, bindingContext);
    
            controllerContext.Controller.TempData["model"] = bindingContext.Model;
        }
    }
    

    and register it in Global.asax:

    ModelBinders.Binders.DefaultBinder = new MyCustomerBinder();
    

    then access it:

    protected override void OnException(ExceptionContext filterContext)
    {
        var model = filterContext.Controller.TempData["model"];
    
        ...
    }
    

    0 讨论(0)
提交回复
热议问题