在OnException MVC中获取ViewModel

时间:2016-05-20 04:08:43

标签: c# asp.net-mvc viewmodel

如何从页面发布的OnException方法中获取ViewModel属性数据。 在我的情况下如果在页面上发生任何错误我使用OnException重定向到错误页面但当我按下后退按钮导航到页面所有字段数据被清除,如果我在onexception方法中获得viewmodel属性我可以重定向到具有这些属性的页面

 protected override void OnException(ExceptionContext filterContext)
        {
            Exception exception = filterContext.Exception;
            //Write Exception to Database
            AppLogging.WriteError(exception);

            string controllerName = (string)filterContext.RouteData.Values["controller"];
            string actionName = (string)filterContext.RouteData.Values["action"];
            // Preserve old ViewData here
            var viewData = new ViewDataDictionary<HandleErrorInfo>(filterContext.Controller.ViewData);

            // Set the Exception information model here
            viewData.Model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
            TempData["ErrModel"] = viewData.Model;           

            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = 500;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "area", "" }, { "action", "Error" }, { "controller", "Error" } });

            // Avoid IIS7 getting in the middle
            Response.TrySkipIisCustomErrors = true;
        }

0 个答案:

没有答案