【问题标题】:View not found error when enabling error pages in ASP.NET MVC project在 ASP.NET MVC 项目中启用错误页面时查看未找到错误
【发布时间】:2015-10-18 00:09:56
【问题描述】:

在我的 ASP.NET MVC 项目中,我将 /errors/error.html 页面作为一般错误页面,并使用第二个 /errors/error404.aspx 处理 404 错误。

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/errors/error.html">
    <error statusCode="404" redirect="/errors/error404.aspx" />
</customErrors>

另外,我的Global.asax 文件中有一个通用处理程序,用于记录所有未捕获的异常以进行日志记录。

public void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    Common.Core.LogError(ex); // Saving exception details in the database
}

上述设置运行良好,但当我遇到一般错误(例如 500 错误)时,Application_Error 在下面记录此错误:

未找到视图“错误”或其主视图,或者没有视图引擎支持搜索到的位置。搜索了以下位置:
~/Views/app/Error.aspx
~/Views/app/Error.ascx
~/Views/Shared/Error.aspx
~/Views/Shared/Error.ascx
~/Views/app/Error.cshtml
~/Views/app/Error.vbhtml
~/Views/Shared/Error.cshtml
~/Views/Shared/Error.vbhtml

根本不记录来自实际错误的异常。

我怎样才能避免这个错误?

附:对于任何可能会问的人,我尝试将我的错误页面作为视图放在控制器中,但我也无法使用 redirectMode="ResponseRewrite" 选项使其工作。

【问题讨论】:

    标签: asp.net asp.net-mvc error-handling web-config custom-error-pages


    【解决方案1】:

    您正试图在 ASP.NET MVC 项目中以非 ASP.NET MVC 方式处理错误。要解决您的问题,您可以在 FilterConfig.cs 文件中注释 filters.Add(new HandleErrorAttribute()); 行。请参阅下面的 sn-p。

    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //filters.Add(new HandleErrorAttribute());
        }
    }
    

    【讨论】:

    • 感谢您的回答,这使它起作用了!是的,我意识到我离 MVC 方式有点远,但当时 MVC 方式似乎有点矫枉过正。
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 2015-01-02
    • 1970-01-01
    相关资源
    最近更新 更多