Scenario: Using ASP.NET membership model to display custom error messages to general users of a website. Send an email to a Webmaster with all the details of such an exception. Also in development environment only detailed exceptions are to be displayed and no custom error messages are displayed.
Environment: VS 2005, ASP.NET 2.0
Requirements:
- The ASP.NET membership and authentication model should be used in the project
- In order to send an email one should be aware of the smtp address to be used
By following the below steps above stated scenario can be achieved:
Step1: Add a Custom Error page to the project and call it is as may be ErrorMessage.aspx
Step2: Add a Global Application class to an existing project and call it as the default Global.asax
Step3: In Global.asax page under Application_Error sub-routine add the following code:
Vb.Net Code
If (Context IsNot Nothing) And (Context.User.IsInRole("Admin")) Then 'check if user is a Admin
Dim err As Exception = Server.GetLastError() 'Get the last error from the server
Response.Clear()
Response.Write("<h1>" & err.InnerException.Message & "</h1>") 'Write error title and the actual error message
Response.Write("<pre>" & err.ToString & "</pre>")
Server.ClearError()
End If
If (Context IsNot Nothing) And (Context.User.IsInRole("Admin") = False) Then 'If user is not a Admin
Dim err As Exception = Server.GetLastError()
Dim msgMail As New MailMessage()
msgMail.From = New MailAddress("abc@xyz.com") 'Mention from email address
msgMail.Subject = "Error"
msgMail.To.Add(New MailAddress("wer@xyz.com")) 'Mention to email address
msgMail.To.Add(New MailAddress("wty@asd.com"))
Dim errorurl As String = Request.Path
Dim ErrorTitle As String = Err.InnerException.Message
Dim ErrorMessage As String = Err.ToString
msgMail.IsBodyHtml = True
Dim strBody As String = "<html><body>" + "An error has occurred. A user requested the page " & errorurl & "<br/>" & ErrorTitle & "<br/>" & ErrorMessage & ". " + " </body></html>"
msgMail.Body = strBody
Dim smtpMail As SmtpClient
smtpMail = New SmtpClient("xyz.com") 'Mention the smtp address
smtpMail.Send(msgMail) 'Send the email
msgMail.Dispose()
Server.ClearError()
Server.Transfer("ErrorMesage.aspx") 'Direct the user to the custom error page
End If
C#.Net Code
<%@ Import Namespace="System.Net.Mail" %>
// Code that runs when an unhandled error occurs
HttpContext context;
context = HttpContext.Current;
Exception err = Server.GetLastError();
//check if user is a Admin
if ((Context != null) && (Context.User.IsInRole("Admin")))
{
//Get the last error from the server
Response.Clear();
Response.Write("<h1>" + err.InnerException.Message + "</h1>");
//Write error title and the actual error message
Response.Write("<pre>" + err.ToString() + "</pre>");
Server.ClearError();
}
if ((Context != null) && (Context.User.IsInRole("Admin") == false))
{
MailMessage msgMail = new MailMessage();
msgMail.From = new MailAddress("abc@xyz.com");
//Mention from email address
msgMail.Subject = "Error";
msgMail.To.Add(new MailAddress("wer@xyz.com"));
//Mention to email address
msgMail.To.Add(new MailAddress("wty@asd.com"));
string errorurl = Request.Path;
string ErrorTitle = err.InnerException.Message;
string ErrorMessage = err.ToString();
msgMail.IsBodyHtml = true;
string strBody = "<html><body>" + "An error has occurred. A user requested the page " + errorurl + "<br/>" + ErrorTitle + "<br/>" + ErrorMessage + ". " + " </body></html>";
msgMail.Body = strBody;
SmtpClient smtpMail = default(SmtpClient);
smtpMail = new SmtpClient("xyz.com");
//Mention the smtp address
smtpMail.Send(msgMail);
//Send the email
msgMail.Dispose();
Server.ClearError();
//Direct the user to the custom error page
Server.Transfer("ErrorMesage.aspx");
}
After adding the code build and test your project by deliberately creating an error in the project. Make sure that the error induced in the project is a run-time error. Try to test both the cases for an Admin and also for a Non-Admin.