Can custom errors automatically redirect to custom error pages according to the error?

Donald Symmons 3,066 Reputation points
2023-03-01T08:22:12.9433333+00:00

In trying to create custom pages in my asp application I found out that this code in the web config file can be used to redirect to its related custom page

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404"/>
      <remove statusCode="500"/>
      <remove statusCode="503"/>
      <error statusCode="404" path="404.aspx" responseMode="Redirect"/>
      <error statusCode="500" path="500.aspx" responseMode="Redirect"/>
      <error statusCode="503" path="503.aspx" responseMode="Redirect"/>
    </httpErrors>
  </system.webServer>

So, I created 3 custom pages (404.aspx, 500.aspx and 503.aspx), and I showed the custom errors for each page as follows

404: 404 Page not found

500: Something went wrong. Please try again later or call Contact Support

503: Site is temporarily unavailable due to maintenance. Please check back later

My question is:

With this custom error pages created and the code in the web config file, will it automatically redirect to the custom pages accordingly, regardless of the server the application is being hosted?

Developer technologies .NET Other
Developer technologies ASP.NET Other
Developer technologies XAML
0 comments No comments
{count} votes

Accepted answer
  1. Alan Farias 755 Reputation points
    2023-03-01T09:58:39.18+00:00

    Yes, with the custom error pages created and the code in the web.config file, the application will automatically redirect to the custom pages according to the error, regardless of the server the application is being hosted on.

    When an error occurs, the ASP.NET runtime looks for the <httpErrors> element in the web.config file and reads the configuration specified for the error codes. The <httpErrors> element specifies how to handle errors that occur during HTTP requests to the application. The errorMode attribute determines whether to display the errors to the client or hide them from the client. If the errorMode attribute is set to "Custom", the <httpErrors> element will redirect the client to the custom error pages specified in the <error> element for the corresponding HTTP error status code.

    Therefore, as long as the web.config file is deployed with the application, and the custom error pages are available, the application will automatically redirect to the custom error pages according to the error, regardless of the server the application is being hosted on.


    Please, if this answer is helpful, click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please let me know.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.