Dec 30, 2007

How to catch the 404 error in my web application and provide more useful information?

In the global.asax Application_error Event write the following code

VB.NET

Dim ex As Exception = Server.GetLastError().GetBaseException()
If TypeOf ex Is System.IO.FileNotFoundException Then
'your code
'Response.Redirect("err404.aspx")
Else
'your code
End If

C#

Exception ex = Server.GetLastError().GetBaseException();
If (ex.GetType() == typeof(System.IO.FileNotFoundException))
{
//your code
Response.Redirect ("err404.aspx");
}
else
{
//your code
}