Implementing a Custom Error page on an ASP.Net website
Asked Answered
O

5

30

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?

Overly answered 29/1, 2010 at 10:58 Comment(1)
I am using IIS 6 on Server 2003.Overly
A
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.

Arquebus answered 29/1, 2010 at 11:18 Comment(2)
If I have the <error> tag outside of the <customError> tags I get an error. I have to put a closing </customError> tag after the error tags but it still does not work.Overly
Got it working when I remembered to change mode to OnMeaning
A
10

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:

Awash answered 1/8, 2014 at 15:9 Comment(1)
web.archive.org/web/20150812092729/http://www.orcsweb.com/blog/… The link is outdated but we can still visit it thanks to the mighty Wayback machineFuze
E
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>

Entwine answered 28/1, 2014 at 13:39 Comment(1)
Carefull setting runAllManagedModulesForAllRequests to true. See this post from hanselman: hanselman.com/blog/…Anchie
G
1
<system.webServer>     
<httpErrors errorMode="DetailedLocalOnly">
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="your page" responseMode="Redirect" />
    </httpErrors>
</system.webServer>
Graehl answered 9/4, 2018 at 13:55 Comment(1)
It's good to add a description to your answer for future referenceBeater
A
-3

Is it a spelling error in your closing tag ie:

</CustomErrors> instead of </CustomError>?
Alost answered 2/5, 2019 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.