Tuesday, May 8, 2012

Global Exception handler

1.Apart from being a terrible user experience unhandled exceptions can also be a security problem. 
implement a global exception handler.To do this, open the Global.asax file
Add code to implement the Application_Error handler as follows.
void Application_Error(object sender, EventArgs e)
     
{
     
Exception myEx =  Server.GetLastError();
   
String RedirectUrlString = "~/Error.aspx?InnerErr=" +
           myEx
.InnerException.Message.ToString() + "&Err=" + myEx.Message.ToString();
     
Response.Redirect(RedirectUrlString);
     
}

2.Then add a page named Error.aspx to the solution and add this markup snippet.
<center>
 
<div class="ContentHead">ERROR</div><br /><br />
 
<asp:Label ID="Label_ErrorFrom" runat="server" Text="Label"></asp:Label><br /><br />
 
<asp:Label ID="Label_ErrorMessage" runat="server" Text="Label"></asp:Label><br /><br /> </center>


3.Now in the Page_Load event handler extract the error messages from the Request Object.
protected void Page_Load(object sender, EventArgs e) {
   
Label_ErrorFrom.Text = Request["Err"].ToString();
   
Label_ErrorMessage.Text = Request["InnerErr"].ToString(); }

No comments:

Post a Comment