带有自定义错误页面的MVC InvalidOperationException

Rya*_*son 10 asp.net-mvc asp.net-mvc-routing

我使用自定义错误页面设置

<customErrors mode="On" defaultRedirect="~/Home/Error">
    <error statusCode="404" redirect="~/Home/PageNotFound" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

我创建了一个抛出异常的页面,然后重定向到正确的错误页面.

但是我注意到生产网络服务器上的elmah中的这些错误:

System.InvalidOperationException未找到视图"错误"或其主文件或视图引擎不支持搜索的位置.搜索了以下位置:〜/ Areas/Football/Views/Draft/Error.aspx~/Areas/Football/Views/Draft/Error.ascx~/Areas/Football/Views/Shared/Error.aspx~/Areas/Football /Views/Shared/Error.ascx~/ Views/Draft/Error.aspx~/Views/Draft/Error.ascx~/Views/Shared/Error.aspx~/Views/Shared/Error.ascx~/Areas/Football /意见/ Draft/Error.cshtml~/Areas/Football/Views/Draft/Error.vbhtml~/Areas/Football/Views/Shared/Error.cshtml~/Areas/Football/Views/Shared/Error.vbhtml~/Views/Draft/Error.cshtml~/Views/Draft/Error.vbhtml~/Views/Shared/Error.cshtml~/Views/Shared/Error.vbhtml

为什么要在别处寻找错误页面?我删除了〜/ Views/Shared/Error.cshtml并在〜/ Home/Error中添加了我的自定义错误页面,因为我在配置文件中指定了一个新的默认值.

有任何想法吗?

谢谢.

Era*_*nga 28

默认情况下,MVC项目会在文件中添加HandleErrorAttributeGlobal.asax.cs

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }
Run Code Online (Sandbox Code Playgroud)

抛出未处理的异常时执行此过滤器.它将视图设置为Error.因此MVC运行时尝试呈现该视图.但在你的情况下,没有这样的观点.因此它再次抛出另一个由ASP.NET运行时处理的异常,并显示您在Web.Config文件中配置的错误页面.

您可以创建自己的异常过滤器并进行注册.

  • _抛出未处理的异常时执行此过滤器.它将视图设置为Error._这是一个非常有价值的信息.非常感谢. (3认同)
  • @Aros - 你需要为Elmah实现一个特殊版本的HandleErrorAttribute.你可以在这里找到一个例子:http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx (2认同)

Der*_*son 5

我最终取消了HandleErrorAttributeGlobal.asax 的注册并只使用了该<customErrors>部分.ELMAH现在可以正确记录错误,我可以指定自定义错误页面.

我错过了什么吗?