【问题标题】:When does ASP.Net MVC4 Error Handling Work?ASP.Net MVC4 错误处理何时起作用?
【发布时间】:2015-12-16 02:07:42
【问题描述】:

我不明白为什么 MVC4 内置的错误处理是这样的。 . .脆弱?

有这个代码:

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

并且在 web.config(system.web 部分)中有这个条目:

<customErrors mode="On"></customErrors>

还有一个如下所示的 Error.cshtml:

@model System.Web.Mvc.HandleErrorInfo
@{
    ViewBag.Title = "Error";
}
<div class="container form-container">
    <div class="row">
    <div class="col-xs-6 col-lg-5 col-lg-offset-1">
        <h1 class="error">Error</h1>
        <h2 class="error">An Error Has Occurred</h2>
        <p>We apologize for the inconvenience.</p>
        @{
        if (Model != null && Model.Exception != null)
        {
            <h3>Exception</h3>
            @if (!string.IsNullOrWhiteSpace(Model.Exception.Message))
            {
            <p>@Model.Exception.Message</p>
            }
            else
            {
            <p>No Excepetion Message was provided.</p>
            }
            @if (!string.IsNullOrWhiteSpace(Model.Exception.StackTrace))
            {
            <h3>Stack Trace</h3>
            <p>@Model.Exception.StackTrace</p>
            }
        }
        else
        {
            <h3>Exception was null</h3>
        }
        }
    </div>
    </div>
</div>

当我试图强制这样的错误时:

[HttpGet]
public ActionResult Create()
{
    throw new ApplicationException("Testing");
    return View();
}

我得到了黄屏死机:

Server Error in '/' Application.

Runtime Error 
  Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed. 

Details: To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".



<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
    <customErrors mode="RemoteOnly"/>
    </system.web>
</configuration>


Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.



<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
    <customErrors mode="On" defaultRedirect="mycustompage.htm"/>
    </system.web>
</configuration>

谁能告诉我我错过了什么?它必须是一些基本的东西。作为记录,我尝试的第一个测试成功了,从那以后我就无法让它工作。我不知道我为打破它所做的任何改变。

【问题讨论】:

  • 你有没有打电话给FilterConfig.RegisterGlobalFilters?检查您的全局应用程序类(global.asax 或 global.asax.cs)。不要以为它很脆弱。很多时候,(开发人员)配置错误是很愚蠢的事情。
  • 是的,FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);在 Application_Start() 中调用。就像我说的,它在我的第一次测试中奏效了。
  • 尝试设置customErrors="Off"。也许有一些逻辑导致了第二个异常,通过查看潜在的错误,您可以诊断它。
  • @mason,是的,我已经尝试了所有可能的设置:On、Off 和 RemoteOnly。他们都给出了黄色的死亡屏幕。 “开”只提供最少的信息。我还确保其他 web.config 文件中没有其他 标记。没有。
  • @mason,我也尝试过使用和不使用默认的_Layout。结果相同。

标签: asp.net asp.net-mvc-4 error-handling


【解决方案1】:

我想通了。它是 Razor 代码块中 if 语句前面的“@”。语法不好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 2016-01-26
    • 2011-08-04
    • 2018-10-07
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多