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 :)
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.
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>
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>