切换到错误视图OnException

时间:2012-12-08 07:16:41

标签: c# razor asp.net-mvc-4

我想重定向到Exception上的其他视图。我有以下代码OnException。但是我怎样才能更改视图?

     protected override void OnException(ExceptionContext filterContext)
     {    // Let other exceptions just go unhandled    
           if (filterContext.Exception is InvalidOperationException) 
           {        // Switch to an error view        ...    
           }
     } 

如何切换到错误视图??

1 个答案:

答案 0 :(得分:1)

ok ..使用以下代码管理:

         var controllerName = (string)filterContext.RouteData.Values["controller"]; 
         var actionName = (string)filterContext.RouteData.Values["action"]; 
         var model = new HandleErrorInfo(filterContext.Exception, controllerName,actionName); 
         filterContext.Result = new ViewResult 
         {             
            ViewName = View,
            MasterName = Master,
            ViewData = new ViewDataDictionary<HandleErrorInfo>(model),  
            TempData = filterContext.Controller.TempData      
        };