1

I am coding an application in ASP.NET.

I would like to know how I can display a custom error page in case the user types the address of a page which does not exist in my project. How can this be done please? Thanks :)

Matt
  • 22,721
  • 17
  • 71
  • 112
Matthew
  • 4,477
  • 21
  • 70
  • 93

3 Answers3

4

You need to handle server error with code 404. There is a plenty of examples over the web (one, two, three), but in short you can do this in web.config with the following section:

<customErrors mode="On" defaultRedirect="~/Error.html">
    <error statusCode="404" redirect="~/Error404.html"/>
</customErrors>

where ~/Error.html and ~/Error404.html are general error page and page for "Resource cannot be found" case.

Community
  • 1
  • 1
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • I tried your solution but I must be missing something. Would you mind taking a look at http://stackoverflow.com/questions/25798775/web-config-not-forwarding-to-404-error-page-on-non-aspx-pages . Thanks – JGallardo Sep 12 '14 at 00:59
2

Add the above to your webconfig

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

kostas ch.
  • 1,960
  • 1
  • 17
  • 30
1

Take a look at the MSDN documentation page for the customErrors element of the web.config schema - the example provided is such:

<configuration>
  <system.web>
    <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly">
      <error statusCode="500" redirect="InternalError.htm"/>
    </customErrors>
  </system.web>
</configuration>
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129