【发布时间】:2018-03-15 02:51:45
【问题描述】:
我有一个标准的 ASP.NET MVC 应用程序。这里我想将所有错误重定向到自定义错误页面。
我已尝试根据我通常做的 + 可以 Google 进行设置,但我仍然没有收到错误。
我有以下 web.config:
<system.web>
<customErrors mode="On" defaultRedirect="~/Error/Index">
<error redirect="~/Error/NotFound" statusCode="404" />
<error redirect="~/Error/Index" statusCode="500" />
</customErrors>
<!-- More stuff :-) -->
</system.web>
<system.webServer>
这是我的错误控制器:
public class ErrorController : Controller
{
public ViewResult Index()
{
return View("Error");
}
public ViewResult NotFound()
{
Response.StatusCode = 404;
return View("NotFound");
}
}
我有以下FilterConfig:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
但是,当我在索引控制器中创建错误时:
public ActionResult Index(InvoiceOverviewViewModel vm)
{
// used to test error pages :D
int a = 5;
int b = 0;
int res = a / b;
}
这给出了一个错误,我看到以下内容:
而我在ErrorController 内的Index.cshtml 具有以下视图:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_VerifyLayout.cshtml";
}
<h1>Danish text that doesn't make any sense here ;-) </h1>
更奇怪的是,我的 404 页面可以正常工作。此页面在 localhost 上完美呈现(不过,not on production)。
有什么想法吗?
【问题讨论】:
标签: c# asp.net-mvc