In asp.net 2.0 web site, what is the best way of writing Error page. I have seen following section at following location:
<customErrors mode="RemoteOnly" defaultRedirect="~/Pages/Common/DefaultRedirectErrorPage.aspx">
void Application_Error(object sender, EventArgs e)
{
}
I am not getting how to use both of them in best way for Error handling.
Please guide me best approach.
BigBlondeViking's response worked great for me except that I found it did not process 403's (which ASP generates when you try to directly access the /Scripts/ or /Content/ directories.) It appears that this is not propegated as an exception and thus not trappable in the Application_Error handling. (this was identified as a "security vulnerability" by an external firm - don't get me started on that!)
I also made a minor change in my common Error Handling. As we are using ASP.NET MVC, I wanted to explicitly call into a controller, as well as pass the exception object. This allowed me to access the exception itself so that I can log / send detailed e-mail depending on the code;
In order to pass the exception OBJECT (not just the message and code) I explicitly invoked the controller:
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
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 ?
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:
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/
EDITS