Article

Implementing a Custom Error page in ASP.Net

30 Jan 2018 Kamal Pratap
0 Comments 682 Views



Today we learn how to manage error screen in asp.Net application when runtime error occurs. By default Asp.net shows a Yellow Screen of Death error page that gives the description about the error along with line number.

 

When an unhandled exception occurs in an ASP.NET application one of three types of error pages is displayed:

  • The Exception Details Yellow Screen of Death error page,
  • The Runtime Error Yellow Screen of Death error page, or
  • A custom error page

A Yellow Screen of Death error page is familiar from the points of developers but from the user's point of view this (YSOD) is terrible. To manage the error conditions we configure the custom error page.

There are 2 ways to configure custom error pages for ASP.NET application

web.config file

<configuration>
    <system.web>
      <customErrors mode="On" defaultRedirect="Error.aspx">
        <error statusCode="400" redirect="400.aspx" />  <!--Bad Request-->
        <error statusCode="401" redirect="401.aspx" />  <!--UnAuthorize Access-->
        <error statusCode="403" redirect="403.aspx" />  <!--Forbidden-->
        <error statusCode="404" redirect="404.aspx" />  <!--Not Found-->
        <error statusCode="500" redirect="500.aspx" />  <!--Internal Server-->
        <error statusCode="503" redirect="503.aspx" />  <!--Service Unavailable-->
      </customErrors>
    </system.web>
  </configuration>

Internet Information Services (IIS) Manager (the GUI)

1.Open Internet Information Services (IIS). select your website and Click on .NET Error Pages icon.

Custom Error Page

Custom Error Page

2.Click on Edit Feature Settings link to enable this feature. Then Edit Error Page Settings dialog box will appear.

Custom Error Page

3. Select the Mode On and enter the customer error page URL in Absolute URL text box. Click OK button.

Custom Error Page

4.If you want to add the error page based on status code then click on Add link to enable this feature.

5. Enter the Status Code and enter the corresponding URL in Absolute URL text box. Click OK button.

Above steps can also be manage in web.config as

<configuration>
    <system.web>
      <customErrors mode="On" defaultRedirect="http://www.freshcodehub.com/error.html">
        <error statusCode="404" redirect="http://www.freshcodehub.com/404.html" />  <!--Bad Request-->      
      </customErrors>
    </system.web>
  </configuration>

 

Kamal Pratap

I have 6+ years experience in .Net technologies like Asp.Net, C#, WCF, Web Services, SQL Server, Ajax, LinQ. Currently I am working in Netcarrots Loyalty Services as a Software Developer.

Comments

No coments found to display!

Leave a Comment