29

I have an ASP.Net website and I want to use a custom error page. I put the following code in my web.config

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

The problem is when i go to a URL that does not exist is still uses the 404 error page specified in IIS Manager.

Question: How can I make it use the error.aspx page I have created? Why do the settings in IIS Manager override the web.config?

Çağdaş Tekin
  • 16,592
  • 4
  • 49
  • 58
Yeodave
  • 978
  • 1
  • 8
  • 15

5 Answers5

33

Try this way, almost same.. but that's what I did, and working.

<configuration>
    <system.web>
       <customErrors mode="On" defaultRedirect="apperror.aspx">
          <error statusCode="404" redirect="404.aspx" />
          <error statusCode="500" redirect="500.aspx" />
       </customErrors>
    </system.web>
</configuration> 

or try to change the 404 error page from IIS settings, if required urgently.

Alex Peta
  • 1,407
  • 1
  • 15
  • 26
Hrushikesh
  • 573
  • 3
  • 12
  • If I have the tag outside of the tags I get an error. I have to put a closing tag after the error tags but it still does not work. – Yeodave Jan 29 '10 at 12:15
  • Got it working when I remembered to change `mode` to `On` – Savage Jul 04 '19 at 11:33
9

There are 2 ways to configure custom error pages for ASP.NET sites:

  1. Internet Information Services (IIS) Manager (the GUI)
  2. web.config file

This article explains how to do each:

The reason your error.aspx page is not displaying might be because you have an error in your web.config. Try this instead:

<configuration>
   <system.web>
      <customErrors defaultRedirect="error.aspx" mode="RemoteOnly">
         <error statusCode="404" redirect="error.aspx"/>
      </customErrors>
   </system.web>
</configuration>

You might need to make sure that Error Pages in IIS Manager - Feature Delegation is set to Read/Write:

IIS Manager: Feature Delegation panel

Also, this answer may help you configure the web.config file:

Community
  • 1
  • 1
JohnB
  • 18,046
  • 16
  • 98
  • 110
  • https://web.archive.org/web/20150812092729/http://www.orcsweb.com/blog/jamie-furr/how-to-create-custom-error-pages-in-iis-7-5-with-asp-net/ The link is outdated but we can still visit it thanks to the mighty Wayback machine – z4nzi Aug 22 '22 at 13:23
3
<customErrors defaultRedirect="~/404.aspx" mode="On">
    <error statusCode="404" redirect="~/404.aspx"/>
</customErrors>

Code above is only for "Page Not Found Error-404" if file extension is known(.html,.aspx etc)

Beside it you also have set Customer Errors for extension not known or not correct as

.aspwx or .vivaldo. You have to add httperrors settings in web.config

<httpErrors  errorMode="Custom"> 
       <error statusCode="404" prefixLanguageFilePath="" path="/404.aspx"         responseMode="Redirect" />
</httpErrors>
<modules runAllManagedModulesForAllRequests="true"/>

it must be inside the <system.webServer> </system.webServer>

RASKOLNIKOV
  • 732
  • 2
  • 9
  • 20
  • Carefull setting runAllManagedModulesForAllRequests to true. See this post from hanselman: https://www.hanselman.com/blog/BackToBasicsDynamicImageGenerationASPNETControllersRoutingIHttpHandlersAndRunAllManagedModulesForAllRequests.aspx – Jelle Oct 05 '20 at 08:54
1
<system.webServer>     
<httpErrors errorMode="DetailedLocalOnly">
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="your page" responseMode="Redirect" />
    </httpErrors>
</system.webServer>
Tommaso
  • 341
  • 4
  • 7
-2

Is it a spelling error in your closing tag ie:

</CustomErrors> instead of </CustomError>?
Siv
  • 125
  • 2
  • 14