How To Provide User Friendly 404 Pages?
Instead of showing ugly "File Not Found" error message you can handle this error with code in global.asax file. You need to write code in Application_Error event:
[ C# ]
void
Application_Error(object sender,
EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception myEx =
Server.GetLastError().GetBaseException();
if (ex.GetType() ==
typeof(System.IO.FileNotFoundException))
{
// File is not found, so redirect visitor to user
friendly 404 page
Response.Redirect("Default404.aspx");
}
}
[ VB.NET ]
Sub
Application_Error(ByVal sender
As Object,
ByVal e As
EventArgs)
' Code that runs when an unhandled error occurs
Dim myEx As
Exception = Server.GetLastError().GetBaseException()
If TypeOf myEx
Is System.IO.FileNotFoundException
Then
' File is not found, so redirect visitor to user
friendly 404 page
Response.Redirect("Default404.aspx")
End If
End
Sub
Related articles: