2017-07-19 121 views
1

我嘗試了所有可能的方式,我在這裏找到(How to make custom error pages work in ASP.NET MVC 4)或在這裏:http://benfoster.io/blog/aspnet-mvc-custom-error-pages,在這裏:http://www.secretgeek.net/custom_errors_mvc但似乎沒有爲我工作。有沒有辦法在ASP MVC 4中啓用自定義錯誤?

我將它簡化爲最小值,但在打擊不存在的頁面時仍會產生醜陋的標準錯誤消息。

我Error.cshtml被放置在視圖\共享文件夾,它看起來像:

<div> 
    <span>Error occurred</span> 
</div> 

嘗試也:

@model System.Web.Mvc.HandleErrorInfo 
@{ 
    ViewBag.Title = "Parking & Ticket Scan Information - Error"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
<div> 
    <span>Error occurred</span> 
</div> 

我的web.config包含:

<customErrors mode="On" defaultRedirect="~/Error"> 
    <error redirect="~/Error/NotFound" statusCode="404" /> 
</customErrors> 

但我也試過:

<customErrors mode="On" defaultRedirect="~/Error"> 
</customErrors> 

我ErrorController是這樣的:

public class ErrorController : Controller 
{ 
    [HandleError] 
    public ViewResult Index() 
    { 
     return View(); 
    } 
} 

(具有或不[的HandleError]屬性,具有和不具有

public ViewResult NotFound() 
{ 
    Response.StatusCode = 404; //you may want to set this to 200 
    return View("NotFound"); 
} 

後來我添加也路由Global.asax中:

路由.MapRoute( name:「PageNotFound」, url:「{* url}」, 默認值:new {controller =「Error」,action =「Index」,id = Ur lParameter.Optional}

沒有似乎強迫IIS顯示自定義錯誤。我還嘗試了什麼?當然註釋掉filters.Add(new HandleErrorAttribute()); 將Error.cshtml移至Views文件夾。

有沒有其他方法可以用來啓用它?

+0

我試過本福斯特的解決方案,它的工作原理。如果IIS首先攔截錯誤,則我嘗試過的其他一些解決方案不起作用 - 在這種情況下,您的應用無法處理錯誤,並且您會看到默認的錯誤。 – Jasen

回答

0

你總是可以做這樣的事情

public class HandleUnauthorizedAttribute : HandleErrorAttribute 
{ 
    public override void OnException(ExceptionContext filterContext) 
    { 
     base.OnException(filterContext); 

     if (filterContext.Exception.GetType() != typeof (SecurityException)) return; 

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

此代碼捕獲所有SecurityExceptions和路線然後用填充HandleErrorInfo自定義錯誤頁。

您可以隨時創建單獨的過濾器,以查找您希望捕獲並路由到其他自定義錯誤頁面的任何異常。

然後在啓動

public class FilterConfig 
{ 
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     filters.Add(new HandleUnauthorizedAttribute()); 
     //more filters if you define them. 
    } 
} 

註冊篩選並創建你的/共享/瀏覽次數

0

期(縣)因此,唯一可能的解決方案,我發現到目前爲止是增加對應於一個視圖在\ View \ Error文件夾中的ErrorController 的動作方法名稱。這個小小的細節似乎在所有其他指南中都沒有。

所以現在我有ErrorContoller。CS:

public class ErrorController : Controller 
{ 
    public ActionResult ErrorDefault() 
    { 
     return View(); 
    } 
} 

而且ErrorDefault.cshtml在查看\文件夾中的錯誤:

@{ 
    ViewBag.Title = "ErrorDefault"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>ErrorDefault</h2> 

而且web.config中:

<customErrors mode="On" defaultRedirect="~/Error/ErrorDefault"> 
</customErrors> 

如果你忘了把你的視圖中對應的子文件夾控制器名稱它將不適用於MVC 4.0。

在RouteConfig中添加單獨的路由不是必需的。

我希望有人會覺得這有用。 :)

+0

「如果您忘記將視圖放入與控制器名稱相對應的子文件夾中,它將無法在MVC中工作」 - 它始終是這樣的。 –

相關問題