Reputation: 1441
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.
Upvotes: 4
Views: 9064
Reputation: 2208
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");
}
Upvotes: 0
Reputation: 37850
As you know, the options are On, Off and RemoteOnly.
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.
Upvotes: 4
Reputation: 57202
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".
Upvotes: 0
Reputation: 31781
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.
Upvotes: 0