我嘗試了所有可能的方式,我在這裏找到(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文件夾。
有沒有其他方法可以用來啓用它?
我試過本福斯特的解決方案,它的工作原理。如果IIS首先攔截錯誤,則我嘗試過的其他一些解決方案不起作用 - 在這種情況下,您的應用無法處理錯誤,並且您會看到默認的錯誤。 – Jasen