System.Web.HttpException / 404 Not Found

  • Thread starter Thread starter Zeb
  • Start date Start date
Z

Zeb

Quick question!

I'm using Global.aspx in my web app to catch unhandled exceptions, log
them and redirect the user to a "something went wrong" page.

This means that at the moment, if the user visits a page that doesn't
exist, they're taken to the generic error page ... but I'd much rather
let them know that the page wasn't found and that they might like to
double check the url.

This is the code I currently have in my Glocal.aspx file:

void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs

if (Context.Error.ToString() != "Thread was being aborted")
{
try
{
MembershipUser user = Membership.GetUser();
int iUserID = 0;
if (user != null)
iUserID = (int)user.ProviderUserKey;
ErrorsBLL errors = new ErrorsBLL();
errors.AddError(Context.Error.ToString(),
Request.ServerVariables["SCRIPT_NAME"], iUserID);
}
finally
{
string root = Config.Root(true);
if (Context.Error is System.Web.HttpException)
Response.Redirect(root + "FileNotFound.aspx");
else
Response.Redirect(root + "Error.aspx");
}
}
}

As you can see, I'm capturing System.Web.HttpExceptions ... but this
isn't ideal because it's not specific to a 404 - so is there any way to
capture a 404 specifically???


Many Thanks,


Matt
 
Here's a snippet rom our Global.asax which accomplishes the same
thing:


if ( ex is HttpException &&
ex.InnerException is FileNotFoundException &&
!File.Exists(Request.PhysicalPath))


HTH,

Sam
 
Brilliant - that's perfect, thanks :)

Here's a snippet rom our Global.asax which accomplishes the same
thing:

if ( ex is HttpException &&
ex.InnerException is FileNotFoundException &&
!File.Exists(Request.PhysicalPath))

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
 
Back
Top