Custom error pages in ASP.NET …

If you have designed even a single application, then you know that it is very difficult if not impossible to code an application that will not generate any sort of exceptions. No matter how hard a developer can try to cover all holes, users will always find way to crash your application and ASP.NET is not an exception to this (I guess that is why testing is crucial component of SDLC.)

In ASP.NET if you get any kind of error you will get really weird message displayed in your web browser. Something like in the screenshot below (Yesss .. I know its IE 9 Just kidding). And with just once glance at it you can imagine how great security risk this type of message can be. If you have some kind of database connection string (for which hopefully I assume that password is not hardcoded Surprised smile), users might be able to see what’s behind door no. 2. Or at least it wont look “good” either.

ASP_Error

And as until your application thoroughly tested by real user, you might never find any weird errors in you app. So, I think it is always good idea to have some kind of mechanism to catch all error that might have missed by your try – catch block !!. In ASP.NET, actually you can design your own error pages called “CUSTOM ERROR PAGES” which kind of provides protection against general errors as well as specific errors (e.g. 400, 401, 403, 500 few to name ). This feature can be really handy if you have really giant application and it may possible that you might have missed some specific scenarios…

For demo purpose, I have made a simple application which basically divides one number with another. And I am trying to divide it by 0. Code snippet is as below.

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Test" onclick="Button1_Click" />
    </div>
    </form>
 protected void Button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            int j = Convert.ToInt32(TextBox1.Text);
            Label1.Text = Convert.ToString( j / i);
        }
 <system.web>
        ........
      <customErrors mode="On" defaultRedirect="~/ErrorPage.aspx">
        <!--
        MODE can be ON,OFF or RemoteOnly. 
        ON will show custom error pages to all.
        OFF will not show custom error page.
        RemoteOnly will show custom error pages to all requests that are NOT LOCAL.
        -->
        <error statusCode="404" redirect="/ErrorPages/404.aspx" />
        <!--    status code specific error redirect  -->
      </customErrors>

        .......
    </system.web>

 

In ASP.NET application, web.config file is used to store all sort of configuration information specific to application. Here, we are required to add one tag between, <system.web> tags called <customErrors> which will allow us to use custom error pages of our choice. Also if the MODE is ON then any request that generates error will be redirected back to custom error page. But if MODE is RemoteOnly then only the requests that are not LOCAL and that generates error will be redirected to custom error page. In real world application, we may find long list of this sort of custom error pages. Actually they can also be used to display kind of uniform error page for any / all kind of errors.

So after implementation of the code, result will look something like below on error. (Yes, its Firefox. IE 9 has some “issues” with loading pagesDon't tell anyone smile)

Rediect

 

That’s it for now.

It’s Just A Thought … Fingers crossed

Gaurang Sign

Leave a Reply

Your email address will not be published. Required fields are marked *