How to e-mail 404 errors
-
I want to be notified of any 404 (Page not found) errors via e-mail. I'm using Windows Server 2008, ASP.NET 3.5, C# code-behind, etc. I have a page, that displays just fine upon 404 error, with the following in code-behind Page_Load():
// Send Email to Admin
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("from@domain.com");
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("to@domain.com");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from,to);message.Subject = "404 - File or directory not found"; message.Body = String.Format( "The following page generated a 404 error:\\n{0}\\n", Convert.ToString(Request.QueryString\["aspxerrorpath"\]) );
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("mail.domain.com", 2525);
try
{
client.Send(message);
}
catch
{
}I've tried various stuff instead of
Request.QueryString["aspxerrorpath"]
, however I always end up with an e-mail message similar to the following:From: from@domain.com [mailto:from@domain.com]
Sent: Friday, May 14, 2010 4:50 PM
To: to@domain.com
Subject: 404 - File or directory not foundThe following page generated a 404 error:
And that's it. How can I figure out what page generated the error so I can e-mail the URL or location? Specifically, given the above example, what should I replace
Request.QueryString["aspxerrorpath"]
with? Any ideas appreciated. Thanks in advance. -
I want to be notified of any 404 (Page not found) errors via e-mail. I'm using Windows Server 2008, ASP.NET 3.5, C# code-behind, etc. I have a page, that displays just fine upon 404 error, with the following in code-behind Page_Load():
// Send Email to Admin
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("from@domain.com");
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress("to@domain.com");
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from,to);message.Subject = "404 - File or directory not found"; message.Body = String.Format( "The following page generated a 404 error:\\n{0}\\n", Convert.ToString(Request.QueryString\["aspxerrorpath"\]) );
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("mail.domain.com", 2525);
try
{
client.Send(message);
}
catch
{
}I've tried various stuff instead of
Request.QueryString["aspxerrorpath"]
, however I always end up with an e-mail message similar to the following:From: from@domain.com [mailto:from@domain.com]
Sent: Friday, May 14, 2010 4:50 PM
To: to@domain.com
Subject: 404 - File or directory not foundThe following page generated a 404 error:
And that's it. How can I figure out what page generated the error so I can e-mail the URL or location? Specifically, given the above example, what should I replace
Request.QueryString["aspxerrorpath"]
with? Any ideas appreciated. Thanks in advance.Nevermind, I figured it out.
Request.RawUrl
orRequest.Url
works fine.