【问题标题】:HttpContext.Current returning null inside ErrorMail_Mailing handler (in Global.asax) for ELMAHHttpContext.Current 在 ELMAH 的 ErrorMail_Mailing 处理程序(在 Global.asax 中)中返回 null
【发布时间】:2013-02-07 12:38:09
【问题描述】:

我们将ELMAH 用于我们的MVC asp.net 应用程序。
当任何exception 发生时,ELMAH 发送具有事件 ErrorMail_Mailing 的错误邮件,
我正在 Global.asax
中为此事件编写处理程序 并尝试从HttpContext.Current.Items 变量中读取值,但得到null exception

任何解决方法请帮助。

我在这里应用 Scott 的建议:- http://scottonwriting.net/sowblog/archive/2011/01/06/customizing-elmah-s-error-emails.aspx 在 global.asax.cs 中,我试图在事件处理程序下方编写,我想在其中配置错误邮件的主题行,其中包含存储在我的 HttpContext.Current.Items

中的内容
  protected void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs e)
        {               
            if (HttpContext.Current.Items["CustomerName"] != null)
            {
                e.Mail.Subject = "Error came to / " + HttpContext.Current.Items["CustomerName"] + " / Error Type: " +
                                 e.Error.Type;
            }
        }

【问题讨论】:

  • 欢迎来到 SO。 :) 你正在尝试在 Gloabal.asax 中编写处理程序?请输入一些代码。
  • 我正在尝试实现以下博客中提到的相同目标:- scottonwriting.net/sowblog/archive/2011/01/06/… 谢谢,我也在更新我的问题。

标签: asp.net-mvc elmah httpcontext


【解决方案1】:

在 MVC 中,它将取决于您在何处设置 HttpContext.Current.Items 值,以确定它是否在 ErrorEmailEvent 中可用。请参考之前的 SO 问题 - HttpContext.Items with ASP.NET MVC 以获取有关正确设置或使用替代方法(如 creating your own ITempDataProvider)的一些指导。

【讨论】:

    猜你喜欢
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    相关资源
    最近更新 更多