asp.net-mvc - HttpContext.Current 在 ELMAH 的 ErrorMail_Mailing 处理程序(在 Global.asax 中)内返回 null

标签 asp.net-mvc elmah httpcontext

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

任何解决办法请帮忙。

我正在应用斯科特在这里建议的内容:- 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;
            }
        }

最佳答案

在 MVC 中,这取决于您设置 HttpContext.Current.Items 的位置。值是否在 ErrorEmailEvent 中可用。请引用之前的SO问题 - HttpContext.Items with ASP.NET MVC有关正确设置此值或使用替代方法(例如 creating your own ITempDataProvider)的一些指导.

关于asp.net-mvc - HttpContext.Current 在 ELMAH 的 ErrorMail_Mailing 处理程序(在 Global.asax 中)内返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14751326/

相关文章:

asp.net - "A potentially dangerous Request.Path value was detected from the client"由 GoogleBot 访问时,但不是通过直接链接

asp.net-mvc - MVC 属性中的 HttpContext - 线程问题?

c# - 访问 ASP.NET Core 2.2 类库中的 HttpContext

ASP.NET MVC 和 ORM 选择

c# - 从 ASP.NET WebForms 迁移到 MVC

smtp - Elmah 在开发服务器上发送错误邮件,但不在生产服务器上发送

c# - 使用 Gmail 进行 ElMAH 错误记录?

asp.net - 从 C# 类访问 ASP.NET 页面的 TextBox 值

javascript - jquery数据表操作按钮

asp.net-mvc - 不区分大小写的模型绑定(bind) MVC 4