【问题标题】:Custom Errors not working with MVC4 project?自定义错误不适用于 MVC4 项目?
【发布时间】:2013-10-05 16:28:09
【问题描述】:

我正在尝试关注这个问题的答案:

How to make custom error pages work in ASP.NET MVC 4

我的 Web.Release.config 中有以下内容:

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

我正在发布模式下运行。我添加了以下控制器:

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

以及控制器操作的视图:

@model System.Web.Mvc.HandleErrorInfo
@{
    Layout = "../Shared/_Layout.cshtml";
    ViewBag.Title = "Error";
}
<div class="list-header clearfix">
    <span>Error</span>
</div>
<div class="list-sfs-holder">
    <div class="alert alert-error">
        An unexpected error has occurred. 
    </div>
</div>

我可以通过手动导航访问这些页面。如果我导航到一些虚假的 URL,我会得到标准的黄色错误页面,其中包含丑陋的诊断信息。

The resource cannot be found.

描述:HTTP 404。您正在寻找的资源(或其之一 依赖项)可能已被删除,名称已更改,或者是 暂时不可用。请查看以下 URL 并制作 确保拼写正确。

请求的网址:/ss

我做错了什么?

【问题讨论】:

  • 你不在 Home 控制器中使用 HandleErrorAttribute 吗?
  • @Andrey.Gubal 是的,所有这些

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

如果启用了自定义错误(如您的示例中所示),并且您使用的是 HandleErrorAttribute,则 ASP.NET MVC 运行时会在当前请求文件夹或共享文件夹中查找 Error.chtml 文件 视图文件夹。在此设置中,defaultRedirect (to GenericErrorPage.htm) 和状态 代码重定向 URI ("~/Error/NotFound")被忽略

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 2012-05-02
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多