How to create/Configure Error page in asp.net web application
When any error was occurred into our asp.net web application it is require that client doesn't know about this error. At that time we need to implement error page redirection and the error details are directly sent to Website administrator vie email.

Error Page Creation Steps

Step 1 – Create Global.asax file into your project and write following code into “Application_Error”

void Application_Error(object sender, EventArgs e)

{

HttpUnhandledException httpUnhandledException = new HttpUnhandledException(Server.GetLastError().Message, Server.GetLastError());

Application["Exception"] = HttpContext.Current.Server.GetLastError();

Application["actualerrorpage"] = httpUnhandledException.GetHtmlErrorMessage();

Application["errortime"] = DateTime.Now.ToLocalTime().ToString();

Application["errorurl"] = Request.Url.AbsoluteUri.ToString();

Application["browser"] = Request.Browser.Browser.ToString() + " ( " + Request.Browser.Type.ToString() + " )";

Application["clientip"] = Request.UserHostAddress.ToString();

if (Request.UrlReferrer != null)

{

Application["referurl"] = Request.UrlReferrer.AbsoluteUri.ToString();

}

else

{

Application["referurl"] = "None";

}

}

Step 2 – Create error.aspx page into your website / Project and write following code into your error.aspx.cs file Page_Load event and “MailToSendSite” function outside of the Page_Load event

==================Page_Load Event Start=====================================

protected void Page_Load(object sender, EventArgs e)

{

if (Application["Exception"] != null && Application["Exception"].ToString() != "")

{

try

{

string mbody = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath).Replace("/", "\\") + "\\mailtemplates\\error.htm");

mbody = mbody.Replace("_ERRROLINK", Application["errorurl"].ToString());

mbody = mbody.Replace("_ERRRODATE", Application["errortime"].ToString());

mbody = mbody.Replace("_ERRORBROWSER", Application["browser"].ToString());

mbody = mbody.Replace("_ERRORDETAILS", Application["Exception"].ToString());

mbody = mbody.Replace("_ERRORIP", Application["clientip"].ToString());

mbody = mbody.Replace("_YELLOWPAGE", Application["actualerrorpage"].ToString());

if (Application["actualerrorpage"].ToString().IndexOf("Validation of viewstate MAC failed") <= 0 && Application["errorurl"].ToString().IndexOf("ScriptResource.axd") <= 0 && Application["errorurl"].ToString().IndexOf("WebResource.axd") <= 0 && Application["actualerrorpage"].ToString().IndexOf("The state information is invalid for this page and might be corrupted") <= 0)

MailToSendSite("ToEmail", "Error in Your Site Name", "ToEmail", mbody.ToString(), null, null, "HostName", "FromEmail", "EmailPassword");

//

}

catch (Exception ex)

{

throw ex;

}

}

}

===================Page_Load Event End===========================================

==================MailSendToSite Function Start======================================

public int MailToSendSite(string tomail, string mailsubject, string frommail, string bodymail, string ccmail, string bccmail, string hostname, string musername, string mpass)

{

try

{

SmtpClient smtpClientC = new SmtpClient();

MailMessage objMailC = new MailMessage();

MailAddress objMailC_fromaddress = new MailAddress(frommail);

MailAddress objMailC_toaddress = new MailAddress(tomail);

objMailC.From = objMailC_fromaddress;

objMailC.To.Add(objMailC_toaddress);

objMailC.IsBodyHtml = true;

objMailC.Subject = mailsubject;

objMailC.Body = bodymail;

if (ccmail != null)

{

if (ccmail != "")

objMailC.CC.Add(ccmail);

}

if (bccmail != null)

{

if (bccmail != "")

objMailC.Bcc.Add(bccmail);

}

smtpClientC.Host = hostname;

smtpClientC.Credentials = new System.Net.NetworkCredential(musername, mpass);

smtpClientC.Send(objMailC);

return 1;

}

catch (Exception ex)

{

HttpContext.Current.Response.Write(ex.StackTrace + "<BR>" + ex.Message + "<BR>" + ex.InnerException);

return 0;

}

}

==================MailSendToSite Function End=======================================

Step 3 – Write following code into Web.config file.

<system.web>

<customErrors mode="On" defaultRedirect="404.html">

<error statusCode="404" redirect="404.html"/>

<error statusCode="400" redirect="404.html"/>

<error statusCode="500" redirect="error.aspx"/>

</customErrors>

</system.web>

Note : Please set mode=”Off” when website / Project is in development phase. Set mode=”On” after “Go Live”

Step 4 – Create error.htm email template in mailtemplate folder for sending email to “admin@yourwebname.com”

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Error</title>

<style>

body {font : 12px Verdana; color : #000000;text-decoration : none;}

.form_text

{

border: 1px solid #000000; font: 11px Verdana; text-decoration: none; background-color: #FFFFFF; cursor: none;

}

.btext

{

font: 14px Verdana; text-decoration: none; background-color: #FFFFFF; cursor: none;font-weight:bold;

}

#outerdiv {position:absolute;top:30%;left:35%;overflow:show;}

A.link:active {color : #97000F; font : bold 11px Verdana; text-decoration : none; }

A.link:link {color : #97000F; font : bold 11px Verdana; text-decoration : none; }

A.link:visited {color : #97000F; font : bold 11px Verdana; text-decoration : none; }

A.link:hover {color : #970000; font : bold 11px Verdana; text-decoration : none; }

</style>

</head>

<body>

<table cellpadding="0" cellspacing="0" width="100%">

<tr><td height="50">&nbsp;</td></tr>

<tr>

<td align="center" width="100%"><img src="_LOGOURL" border="0" /></td>

</tr>

<tr><td>&nbsp;</td></tr>

<tr>

<td align="center" class="btext">Exception raised</td>

</tr>

<tr><td>&nbsp;</td></tr>

<tr>

<td>

<table border="0" cellpadding="0" cellspacing="0" width="100%">

<tr>

<td width="20%" class="btext" valign="top">Link</td>

<td valign="top">_ERRROLINK</td>

</tr>

<tr>

<td colspan="2">&nbsp;</td>

</tr>

<tr>

<td class="btext" valign="top">Date</td>

<td valign="top">_ERRRODATE</td>

</tr>

<tr>

<td colspan="2">&nbsp;</td>

</tr>

<tr>

<td class="btext" valign="top">Browser</td>

<td valign="top">_ERRORBROWSER</td>

</tr>

<tr>

<td colspan="2">&nbsp;</td>

</tr>

<tr>

<td class="btext" valign="top">Error Details</td>

<td valign="top">_ERRORDETAILS</td>

</tr>

<tr>

<td colspan="2">&nbsp;</td>

</tr>

<tr>

<td class="btext" valign="top">IP Address</td>

<td valign="top">_ERRORIP</td>

</tr>

<tr>

<td colspan="2">&nbsp;</td>

</tr>

<tr>

<td colspan="2" class="btext" valign="top">Yellow Page</td>

</tr>

<tr>

<td colspan="2">&nbsp;</td>

</tr>

<tr>

<td colspan="2">_YELLOWPAGE</td>

</tr>

</table>

</td>

</tr>

</table>

</body>

</html>
0

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month