我正在使用 VS2010(使用 C#)。我有一个在线申请。我有 2 个错误表单设置,如果用户遇到错误,他们将被重定向到。我有一个 global.asax.cs 文件,我有代码设置,Application_Error
以便向我发送电子邮件,让我知道用户收到了错误。我的问题是每次表单加载时我都会收到一封电子邮件。应该只在发生错误时。这是我在global.asax.cs
文件中的代码:
protected void Application_Error(object sender, EventArgs e)
{
if (HttpContext.Current.Server.GetLastError() != null)
{
Exception myException = HttpContext.Current.Server.GetLastError().GetBaseException();
// Create the Outlook application by using inline initialization.
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem email = (Outlook.MailItem)(oApp.CreateItem(Outlook.OlItemType.olMailItem));
email.Recipients.Add(fake@fakeemail.com);
email.Subject ="Error in COF page " + Request.Url.ToString();
string message = string.Empty;
message +="<b>Message</b><br />" + myException.Message + "<br />";
message +="<b>StackTrace</b><br />" + myException.StackTrace + "<br />";
message +="<b>Query String</b><br />" + Request.QueryString.ToString() + "<br />";
email.Body = message;
email.Send();
//Clear out any lingering errors after they have been reported.
Server.ClearError();
}
}