4

How to get ViewModel properties data in OnException method that is posted from the page. In my Case If any error occurs on page i redirect to Error Page using OnException but when i press back button to navigate to the page all fields data gets cleared, if i get viewmodel properties in onexception method i can redirect to the page with these properties.

 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;
        }
Bokambo
  • 4,204
  • 27
  • 79
  • 130

0 Answers0