【问题标题】:Can the ASP.NET MVC Yellow Screen of Death (YSOD) be generated on demandASP.NET MVC 黄屏死机(YSOD)能否按需生成
【发布时间】:2011-05-14 21:11:38
【问题描述】:

如问here

我想知道是否可以在不使用 ELMAH 的情况下通过邮件发送异常的 YSOD 的 HTML 渲染?我正在处理错误并向用户显示自定义错误页面。我也通过邮件发送异常一般信息,但是我真的很想知道我是否可以将它们包装到 ASP.NET 的真正内置 YSOD 引擎中并保持 HTML 格式。

更新1:

我有我的自定义异常 (DupplicatedArbsException),它返回一个带有我认为是“托管异常”消息的视图。但是,如果是我没有捕捉到的真正错误,它将返回错误视图。

    [HandleError(ExceptionType = typeof(Exception), View = "Error")]
    [HandleError(ExceptionType = typeof(DuplicatedArbsException), View = "ErrorViewArbs")]
    public ActionResult Create(string id, int? version)
    {
         //...
    }

HandleError 引发当前什么都不做。

    protected override void OnException(ExceptionContext filterContext)
    {
        var ex = filterContext.Exception;
        base.OnException(filterContext);
    }

..

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

customErrors mode="off" 中引发的异常是来自 asp.net 的 YSOD。但是,当我打开 customErrors mode="on" 时,这些异常没有包含在它的 html 等效项中,而只是异常消息(根本没有 html)。

【问题讨论】:

  • 帮助您进行搜索:YSOD 是 ASP.NET 引擎的一部分,而不是 .NET 框架。

标签: asp.net-mvc exception-handling yellow-screen-of-death


【解决方案1】:

您可以处理 global.asax 中的 Application_Error 事件,该事件由 ASP.NET 引擎在每次未处理异常时触发:

protected void Application_Error(object sender, EventArgs e)
{
    var app = (HttpApplication)sender;
    var context = app.Context;
    // get the exception that was unhandled
    Exception ex = context.Server.GetLastError();

    // TODO: log, send the exception by mail
}

【讨论】:

  • 由于我确实处理了所有异常,因此永远不会引发 Application_Error。此外, context.Server.GetLastError() 始终为空。
  • 如果您处理所有异常,您将永远不会看到 YSOD。
  • 我只想拦截 YSOD 异常,通过电子邮件将其发送给站点管理员,然后将非 YSOD 消息发送给用户。
  • 通常在 Application_Error 处理程序中,您可以拦截异常。您还可以查看ELMAH,这将大大简化此任务。一个优点是您不需要修改现有代码。您只需在 web.config 中插入一个部分。
  • 好的,我在异常时取消了我的handleError,所以现在引发了application_error,我可以捕获它,现在从HttpApplication,我可以从YSOD访问HTML吗?
猜你喜欢
  • 2010-10-27
  • 2011-01-21
  • 2014-03-12
  • 2011-07-23
  • 2011-06-24
  • 2011-01-11
  • 2010-09-06
  • 2012-10-27
  • 1970-01-01
相关资源
最近更新 更多