【发布时间】:2017-12-25 03:14:28
【问题描述】:
我尝试了所有可能的方法,我在这里 (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,但似乎没有一个对我有用。
我将它简化为最小,但在点击不存在的页面时仍然收到丑陋的标准错误消息。
我的 Error.cshtml 放在 Views\Shared 文件夹中,它看起来像:
<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 中添加了路由:
routes.MapRoute( 名称:“PageNotFound”, 网址:“{*url}”, 默认值:new { controller = "Error", action = "Index", id = UrlParameter.Optional }
似乎没有什么可以强制 IIS 显示自定义错误。我还尝试了什么?当然注释掉了 filters.Add(new HandleErrorAttribute()); 将 Error.cshtml 移至 Views 文件夹。
我可以使用其他方法来启用它吗?
【问题讨论】:
-
我已经尝试过 Ben Foster 的解决方案并且它有效。如果 IIS 首先截获错误,我尝试过的其他一些解决方案将不起作用——在这种情况下,您的应用将无法处理错误,您将看到默认错误。
标签: c# asp.net-mvc asp.net-mvc-4 custom-errors