+5  A: 

In my global asax i always check to see what type of http error it is...

then transfer to the correct error page specified in web.config I like to handle the usual suspects, 404 ( lost page ) and 500 ( server error )

some background on http status code is importaint to know why they are handled:

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

my web.config look something like this

<customErrors mode="On"  defaultRedirect="~/error.aspx"  >
  <error statusCode="404" redirect="~/lost.aspx"  />
  <error statusCode="500" redirect="~/error.aspx"  />
</customErrors>

my lost page has logic in it to attempt to find a link to the page that they might have been looking for, as well as some other formatting.

my error page is a bit different, showing some error messages ,

so i handle both differently.

depending if you have secured areas of your site you might want handle the 401/403 ?

protected void Application_Error(object sender, EventArgs e)
{
    var context = Context;


    var error = context.Server.GetLastError() as HttpException;
    var statusCode = error.GetHttpCode().ToString();

    // we can still use the web.config custom errors information to
    // decide whether to redirect
    var config = (CustomErrorsSection)WebConfigurationManager.GetSection("system.web/customErrors");
    if (config.Mode == CustomErrorsMode.On ||
        (config.Mode == CustomErrorsMode.RemoteOnly && context.Request.Url.Host != "localhost"))
    {
        // set the response status code
        context.Response.StatusCode = error.GetHttpCode();

        // Server.Transfer to correct ASPX file for error
        if (config.Errors[statusCode] != null)
        {

            HttpContext.Current.Server.Transfer(config.Errors[statusCode].Redirect);
        }
        else
            HttpContext.Current.Server.Transfer(config.DefaultRedirect);
    }
}

the reason i server transfer is so search engines don't get confused, and to keep my webmaster logs meaningful... if you redirect you return a http status 302 which tell the browser to go to the page redirected to... then this next page returns a status code 200 ( ok ).

302 --> 200 , or even 302 --> 404 has different meaning that just a 404...

then on say my 404 error page i make sure i set the status code of the http error:

protected void Page_PreRender(object sender, EventArgs e)
{
    Response.Status = "404 Lost";
    Response.StatusCode = 404;

}

This article was helpful to me, I knew what I wanted to do but I like how this code looked at the web.config settings... http://helephant.com/2009/02/improving-the-way-aspnet-handles-404-requests/

Return the right status code

By default the page handling a 404 error page doesn’t return a 404 status code to the browser. It displays the error message that you provided to the user but doesn’t have any extra information to flag the page as an error page.

This is called a soft 404. Soft 404 pages aren’t as good as ones that return the 404 status code because returning the 404 status code lets anything accessing your file that the page is an error page rather than a real page of you site. This is mostly useful for search engines because then they know they should remove dead pages from their index so users won’t follow dead links into your site from results pages.

Pages that return 404 status codes are also useful for error detection because they’ll be recorded in your server logs so if you have unexpected 404 errors, they’ll be easy to find. Here’s an example of the 404 error report in Google Webmaster tools:

EDITS

Is it require to write server.clearerror() in global.asax? What does it impact

  • No, You can do it on you error pages, not sure the impact? if you do a transfer non, if you redirect, there might be the possibility another error happened between the requests? I don't know

Why in web.config we should write error.aspx twice one with status code 500 and another is defaultredirect

  • I use 2 because a lost page should show/and do different things than a server error. the error page shows the user there was an error that we could not recover from... and its probably our fault. I leave a default redirect for any of the other error codes as well. 403,401, 400 ( they are more rare, but should be handled )

Can you also tell me the code of error.aspx and lost.aspx.

  • this depends on the type of website you have. you get the error the same way, but what you do with it is up to you. on my lost page i search for some content the user might have been looking for. the error pages i log the error and so a user friendly oops page... you will need to figure out what is needed.
BigBlondeViking
Hi, Thanks for nice information. Can you also tell me that what is the advantage of writing Response.Status = "404 Lost"; Response.StatusCode = 404; and second Should we write two redirect pages in web.config?
Hemant Kothiyal
expanded a little... look at some of the resources i added.
BigBlondeViking
Thanks, Now i think i got that why server.transfer required in application_error page, so that getlasterror will accessible at error.aspx/lost.aspx. If we doesn't write explicitly server.transfer, then also error page will show but by using response.redirect which cannot access last error. Here i would like to know three points (1) Is it require to write server.clearerror() in global.asax? What does it impact (2) Why in web.config we should write error.aspx twice one with status code 500 and another is defaultredirect (3) Can you also tell me the code of error.aspx and lost.aspx.
Hemant Kothiyal
Hi sir, I am waiting for your help and information
Hemant Kothiyal
No, You can get the error with response redirect, but you return a 302 to the client, the 302 tells the cleint to then go to the error page... its and extra step that is not as good as a server tranfer that only returns the 1 time... ( read the article i linked to...)
BigBlondeViking
Hi sir, I am very thankfull to you for guiding me in such a nice way. As much i read your reply my curiosity increase for more. I have parallel read microsoft msdn. There i found following link http://msdn.microsoft.com/en-us/library/bb397417.aspx Can you please explain me (1) Compare your solution with Microsoft(2) As per i learn i found your application error good but I found few good thing at microsoft link ?(3) Can you please guide me to use it in best way?
Hemant Kothiyal
Hi sir, Had you gone with the link i send you. What do you think now
Hemant Kothiyal