2016-12-08 62 views
0

我使用的解決方案過濾器發佈到Clean way to catch all errors thrown in an MVC 3.0 application?,我想知道如何重定向到一個視圖(駐留在/ Views/Account/ErrorPage中的簡單錯誤頁面,作爲該帳戶控制器),在該過濾器?它被轉載如下:從異常處理過濾器重定向

public class HandleExceptionsAttribute : HandleErrorAttribute 
    { 
     public override void OnException(ExceptionContext filterContext) 
     { 
      var expception = filterContext.Exception; 

      // Go to generic error page: 

     } 
    } 

謝謝。

回答

0

設置filterContext.Result

 filterContext.Result = new ViewResult 
     { 
      ViewName = "<view name>", 
      ViewData = new ViewDataDictionary<HandleErrorInfo>(model), 
      TempData = filterContext.Controller.TempData 
     }; 

下面是捕獲調用的錯誤

public override void OnException(ExceptionContext filterContext) 
    { 
     base.OnException(filterContext); 

     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 name>", 
      ViewData = new ViewDataDictionary<HandleErrorInfo>(model), 
      TempData = filterContext.Controller.TempData 
     }; 
     filterContext.ExceptionHandled = true; 
     filterContext.HttpContext.Response.Clear(); 
     filterContext.HttpContext.Response.StatusCode = 500; 
     filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
    } 
+0

應該是什麼的' 「」'語法完整的方法是什麼?我嘗試了'「/ Account/ErrorPage」'但是這導致錯誤「沒有找到主人或者沒有視圖引擎支持搜索到的位置......」 – ITWorker

+0

將您的視圖置於共享視圖中 – Fran