Implementation of Custom Error Page in. net

  • 2021-10-25 06:26:40
  • OfStack

Foreword:

In actual web development, the following situations are often encountered, resulting in a bad experience:

a, the program does not handle the exception, direct output display to the user page

b, the resource accessed by the user does not exist, and the default 404 page of the system is displayed directly

c, other system default pages for requesting error status (403, etc.)

In order to give a user-friendly experience, in actual project development,

You need to customize the corresponding friendly prompt page for different exceptions in the system

. net custom exception page redirection is through web. config configuration page configuration implementation, its specific implementation has two ways, the following words do not say much, let's take a look at a detailed introduction.

It is realized by adding customErrors configuration node to node system. web

IIS Environment Requirements: IIS7, IIS7 +, IIS7 Previous

Action object: Error handlers that act on the Asp. Net level

That is, it does not work for static resources such as (. html/. js)

Implementation method:


<system.web>
 <customErrors mode="On" defaultRedirect="ApplicationErroy.aspx">
  <error statusCode="403" redirect="/ErrorPage/403.html"/>
  <error statusCode="404" redirect="/ErrorPage/404.html"/>
  <error statusCode="500" redirect="/ErrorPage/500.html"/>
 </customErrors>
 </system.web>

2. It is realized by adding httpErrors configuration node to node system. webServer

IIS Environmental Requirements: IIS7, IIS7 +

Action object: Error message handler that acts on IIS level

That is, asp. net program exceptions and static resource exceptions are handled

Implementation method:


<system.webServer>
 <httpErrors errorMode="Custom" existingResponse="Replace">
  <clear />
  <error statusCode="404" responseMode="ExecuteURL" path="/ErrorPage/404.html" />
  <error statusCode="403" responseMode="ExecuteURL" path="/ErrorPage/403.html" />
  <error statusCode="500" responseMode="Redirect" path="/ErrorPage/500.html" />
 </httpErrors>
 </system.webServer>

Application summary:

According to the above description, in actual development, as long as the environment is IIS7 +, the second method can be completely adopted

Summarize


Related articles: