Freigeben über


customErrors defaultRedirect is not redirecting in ASP.NET

In one of my projects I was trying to use customErrors in ASP.NET and it was not working as I would expect. In the web.config I had the following...

<customErrors defaultRedirect="~/Misc/ErrorPage.aspx" mode="On">
   <error statusCode="404" redirect="~/Misc/ErrorPage.aspx" />
</customErrors>

Here is how my virtual directory TestingRedirection looks like...

I have an error page called ErrorPage.aspx in a subfolder called Misc.

If I browse Default.aspx, everything is fine. But, if try https://localhost/testingredirection/default11.aspx, it would throw me the following error message (HTTP Error 404 - File or directory not found)...

Ideally, I should get my custom error page  (ErrorPage.aspx) in the Misc folder. Let's have a look at the IIS log files when it shows 404 error message...

2007-05-18 19:39:08 W3SVC1 RAHULSERVER 127.0.0.1 GET /TestingRedirection/Default11.aspx - 80 FAREAST\rahulso 127.0.0.1 HTTP/1.1 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.2;+.NET+CLR+1.1.4322;+InfoPath.2;+.NET+CLR+2.0.50727) - - localhost 404 0 2 1795 450 0

IIS simply told us, that I don't have that file and thus a 404. So, how will ASP.NET be able to take care of it??

Let's go ahead and fix this issue and see everything in action.

1. Open IIS Manager, right click on the VD where you are having issues and click on Properties
2. In the Directory tab, click on Configuration button and double click on aspx in the Application Extensions
3. You will get the dialog as shown below and you need to uncheck that guy Verify that file exists. If it is checked IIS will verify the request and thus the 404.

Once you are done with this, you should ideally be able to redirect to the custom error page. Note how the IIS Logfile looks like now...

2007-05-18 19:25:46 ::1 GET /TestingRedirection/Default.aspx - 80 - ::1 Mozilla/4.0+ 200 0 0    -> Default.aspx exists
2007-05-18 19:25:53 ::1 GET /TestingRedirection/Default11.aspx - 80 - ::1 Mozilla/4.0+ 302 0 0    -> Default11.aspx DOES Not exist, but in that case it is redirecting to the error page as per our web.config as we can see below
2007-05-18 19:25:53 ::1 GET /TestingRedirection/Misc/ErrorPage.aspx aspxerrorpath=/TestingRedirection/Default11.aspx 80 - ::1 Mozilla/4.0+ 200 0 0  -> OKay

Until next time Wave
Rahul

Share this post :

Comments

  • Anonymous
    August 12, 2008
    hi, my checkbox Verify that file exists is unchecked.  what else can be wrong?? Please help. Thanks

  • Anonymous
    November 18, 2008
    There is one issue with this, which you've highlighted above with your HTTP headers. A 404 redirect should return a 404 status. If it doesn't, search engines get upset, and rightfully so. Also, the nature of the ASP.NET pipeline means that under normal operation, things like missing .jpg images return a 404 that doesn't get processed through the asp.net pipeline, which causes different error behavior for .aspx 404s and 404s with just about every other file extension, which I'm not a fan of at all. I would think a more comprehensive error handling solution is necessary than just reconfiguring the IIS verification flag.