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?

3
  • Are you using the integrated pipeline mode in IIS?
    – d4nt
    Jun 9, 2011 at 9:16
  • yes. i am. will that be a problem? Jun 9, 2011 at 9: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 9, 2011 at 9:23

2 Answers 2

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.

9
  • Does this setting get erased after a new release? Dec 30, 2016 at 6:49
  • I don't know what you mean by a new release.
    – ErikE
    Dec 30, 2016 at 7:24
  • Means i publish the web app to server Dec 30, 2016 at 7:43
  • Are you overwriting applicationhost.config? Don't do that.
    – ErikE
    Dec 30, 2016 at 7:46
  • 1
    @It'satrap The httpErrors section is a global IIS setting, so just publishing doesn't affect the config.
    – Kapé
    Jan 5, 2017 at 17:58
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.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.