16

Coding Platform ASP.NET 4.0 WebForms on IIS7.5

If I request a page blahblah.aspx which does not exist, I get navigated to my custom error page for Page not found Exception. I have implemented URL routing and so I have omitted all my .aspx extensions for routes.

Now, If I type www.mysite.com/blahblah (thats a url without extension) instead of going to my custom error page, it gets redirected to my IIS 404 error page.

What should I do to make my custom error page appear on my site instead of IIS error page?

ErikE
  • 48,881
  • 23
  • 151
  • 196
naveen
  • 53,448
  • 46
  • 161
  • 251
  • Are you using the integrated pipeline mode in IIS? – d4nt Jun 09 '11 at 09:16
  • yes. i am. will that be a problem? – naveen Jun 09 '11 at 09:20
  • No, it's fine, it's just helpful to figure out where the problem might be. In classic mode, IIS can return error codes without the request ever making it as far as your app, based purely on what files it can see on the disk. Of course, in MVC it's not serving up files so that would be a problem. Integrated mode means the request will be getting to your app. – d4nt Jun 09 '11 at 09:23

2 Answers2

35

The Easy Way

If your IIS 7 web server doesn't already have it, install the IIS 7.0 Administration Pack from Microsoft.

Navigate to the root web server name in IIS, and open the Configuration Editor (part of the Administration Pack). Change the dropdown to system.webServer/httpErrors, right-click on defaultPath, and choose 'defaultPath' Attribute -> Unlock Attribute.

Unlocking the defaultPath attribute

Then try to change the custom error handler page again. Navigate to your site, open Error Pages under the IIS group, click Edit Feature Settings on the right, select Custom error pages and finally, put in your path for the default page.

Setting custom error pages

No lock error this time.

I originally tried to unlock this attribute at the web site level but was prevented, so be aware you may have to go all the way to the root of the IIS tree, the web server itself.

The Manual Way

I know you can accomplish all this by direct editing in notepad of the appropriate config file on the web server. And that may be required for your particular web hosting environment or company production web server change protocols. But why make it complicated if it doesn't need to be? Plus, this way you can do it in the GUI, and compare the before and after to see what changes you truly have to make. But if you MUST do it manually, then:

Open the file %windir%\System32\inetsrv\config\applicationHost.config in Notepad. Run Notepad as administrator if you're having problems.

Pro tip: Do not use notepad++ to edit applicationHost.config. You'll end up secretly and silently saving a 32-bit copy of the file that's in a different folder than the one IIS looks in (due to Windows, not np++) without actually affecting the file you wish to change.

You'll see something like this:

<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">

Remove the ,defaultPath section and save.

You will be able to make the changes you need.

ErikE
  • 48,881
  • 23
  • 151
  • 196
5

Assuming you haven't done this already...

Go to IIS, select your website, and choose the option "Error Pages". In there, find the 404 status code, right click and go to "Edit Feature Settings". you can then select "Custom Error pages" and change the "Path Type" to URL "Execute URL" to do some rewriting, or a default redirect page for anything else. Put in your URL as a relative value, and now it should redirect to this url on 404 errors.

Edit: Did a bit of digging, in cases, this config may be locked, if you run the following command, it should remove the lock:

%windir%\System32\inetsrv\appcmd unlock config -section:system.webserver/httperrors

Once that is done, you can edit the url properly.

Oriacle
  • 244
  • 1
  • 9
  • Hrm, it may be that parts of the IIS config are locked. Check out [http://learn.iis.net/page.aspx/145/how-to-use-locking-in-iis-70-configuration/](http://learn.iis.net/page.aspx/145/how-to-use-locking-in-iis-70-configuration/) – Oriacle Jun 09 '11 at 09:43
  • Updated answer with a solution to that problem. – Oriacle Jun 09 '11 at 09:47
  • @Oriacle - invalid link in your above comment – thecoolmacdude Jul 02 '15 at 18:27