ErrorPage from ErrHandler

  • Thread starter Thread starter Eje
  • Start date Start date
E

Eje

I'm designing a 3-tier web application in .NET. In the
middle tier I have an error handler. I use Try-Catch
where I feel it's necessary. If I get a critical error I
would like to redirect to a "Critical Error page"
directly from the error handler without going up to the
UI tier. Is that possible and in that case how does the
code look?

Eje
 
Hi Eje,

Another alternative is to inherit your page from a custom defined class that
inherits from System.Web.UI.Page. In that class, you must hook to the
Page_Error event.

That class (a regular c# class) should be something like :

public class MyBasePage: System.Web.UI.Page
{
public BasePage()
{
this.Error += new System.EventHandler(this.Page_Error);
}

protected void Page_Error(Object source, EventArgs e)
{
// put your code here
// to get access to the last exception use Server.GetLastError()
}
}

Then, you must replace in your page this

public class yourPageName: System.Web.UI.Page

with

public class yourPageName: MyBasePage


In the base page you can also perform any operation that is common to all of
your pages (or at least the ones that inherit from it).

You can get more info about this technique her
http://msdn.microsoft.com/library/d...y/en-us/dnpatterns/html/ImpPageController.asp

Another possibility is to use HttpModules and HttpHandlers. You can find a
great example of this technique here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/elmah.asp

Regards,
Leon
 
Back
Top