views:

383

answers:

4

I have an asp.net application.When I opened the application,I got a runtime exception with the description saying, "An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code".

When ever I get this kind of runtime exception, I want to redirect page to some other aspx page.I have added the custom error tag in web.config file as :

<customErrors
   mode="RemoteOnly" 
   defaultRedirect="~/Error.aspx" />

But still m getting runtime error page instead of Error page.

A: 

Make sure you're customErrors mode is On, as opposed to Off or RemoteOnly. The latter option seems to be the default, but will only work when the page requests are being made locally.

David Andres
A: 

RemoteOnly Specifies that custom errors are shown only to the remote clients, and that ASP.NET errors are shown to the local host (from msdn).
Are you seeing those errors on the development machine? If so, then it's the correct behavior, and you might try with customErrors="off".

Paolo Tedesco
+2  A: 

As you know, the options are On, Off and RemoteOnly.

  • RemoteOnly means that if you are browsing locally, you will not get the custom error page; you will get ASP.NET's in-built exception page. If you are browsing remotely (IE, not on the physical machine on which the app is being served), then you will see Error.aspx. I almost never use this setting anymore.
  • On means that no matter what, if there is an error, you will see Error.aspx. I tend to use On in production environments.
  • Off means that no matter what, you will see ASP.NET's in-box error page (the exception page.) Off is not recommended for production, but is usually what I use on test servers, as I will want my stack trace. :)

All of this is predicated on Error.aspx not throwing its own exception, of course. If Error.aspx throws its own unhandled exception, then all bets are off -- you're going to get a default ASP.NET exception page.

John Rudy
A: 

set up an Application_Error event handler in my Global.asax file and I have implemented a server transfer for errors. Now I want to set up a specific page to handle 404 errors. How do I detect a 404 error and call the 404-specific page?

The main answer to this question involves retrieving the exception that triggered the event in the first place. To do that, we call Server.GetLastError():

What we need to do next is determine if the exception is an HttpException or something else. Once we have determined that it is an HttpException we will have access to Http-specific properties and methods that will give us the rest of the information we are looking for. In our case we want to call the GetHttpCode() method, which will return the Http status code and compare it to the number 404.

void Application_Error(object sender, EventArgs e)

{

Exception ex = Server.GetLastError();

if (ex is HttpException)

{

    if (((HttpException)(ex)).GetHttpCode() == 404)

        Server.Transfer("~/Error404.aspx");

}

// Code that runs when an unhandled error occurs

Server.Transfer("~/DefaultError.aspx");

}

amexn