Wednesday, July 10, 2013

How to handle Custom Error in Asp.net OR Custom Error Handling in Asp.net

Introduction: In this post I will try to explain how we can Handel the error in Asp.net.
Custom Error Handling

Description:
Sometimes in application we would not want to show Error in message i.e. page not found, server is busy etc. when Error generates on application that time application redirect to default error page.
Take a new website and in web.config file write the below mention code:
<configuration>
       <system.web>
              <compilation debug="true" targetFramework="4.0"/>
              <customErrors defaultRedirect="404.aspx" mode="On">
                     <error statusCode="400" redirect="404.aspx"/>
      <error statusCode="500" redirect="505.aspx"/>
              </customErrors>
       </system.web>
</configuration>

400 (4XX) Errors : Client Side
500 (5XX) Errors: Server Side
Note: if any error will comes in application is will redirect the user to 404.aspx.
Add a webform to project. Here I keep its name 404.aspx and design as mention below:
<body style="background-color:#eaeaea;">
    <form id="form1" runat="server">
    <div>
    <table><tr><a href="Default.aspx">Home</a></tr></table>
    <table style="margin-left:380px;margin-top:50px"><tr><td><a href="#"><img src="Images/404.png" /></a></td></tr></table>  
    </div>
    </form>
</body>

Also add another webform for 500 Error (500.aspx) redirect as mention below:
<body style="background-color:#eaeaea;">
    <form id="form1" runat="server">
    <div>
      <div class="css_table_cell">
          <p align="CENTER" class="page_heading">HTTP Error 500 Internal server
            error</p>
          <p><b>Introduction</b></p>
          <blockquote>
            <p>The Web server (running the Web Site) encountered an unexpected
              condition that prevented it from fulfilling the request by the
              client (e.g. your Web browser or our CheckUpDown robot) for access
              to the requested URL.</p>
            <p>This is a 'catch-all' error generated by the Web server.
              Basically something has gone wrong, but the server can not be more
              specific about the error condition in its response to the client.
              In addition to the 500 error notified back to the client, the Web
              server should generate some kind of internal error log which gives
              more details of what went wrong. It is up to the operators of the
              Web server site to locate and analyse these logs. </p>
          </blockquote>
          <p><b>500 errors in the HTTP cycle</b></p>
          <blockquote>
            <p>Any client (e.g. your Web browser or our CheckUpDown robot) goes
              through the following cycle when it communicates with the Web
              server:</p>
            <ul>
              <li>Obtain an IP address from the IP name of the site (the site
                URL without the leading 'http://'). This lookup (conversion of
                IP name to IP address) is provided by domain name servers
                (DNSs). </li>
              <li>Open an IP socket connection to that IP address. </li>
              <li>Write an HTTP data stream through that socket. </li>
              <li>Receive an HTTP data stream back from the Web server in
                response. This data stream contains status codes whose values
                are determined by the HTTP protocol. Parse this data stream for
                status codes and other useful information. </li>
            </ul>
            <p>This error occurs in the final step above when the client
              receives an HTTP status code that it recognises as '500'. (Last
              updated: March 2012).</p>
          </blockquote>
          <p><b>Fixing 500 errors - general</b></p>
          <blockquote>
            <p>This error can only be resolved by fixes to the Web server
              software. It is not a client-side problem. It is up to the
              operators of the Web server site to locate and analyse the logs
              which should give further information about the error.</p>
          </blockquote>
          <p><b>Fixing 500 errors - CheckUpDown</b></p>
          <blockquote>
            <p>Please contact us (email preferred) whenever you encounter 500
              errors on your CheckUpDown account. We then have to liaise with
              your ISP and the vendor of the Web server software so they can
              trace the exact reason for the error. Correcting the error may
              require recoding program logic for the Web server software, which
              could take some time. </p>
          </blockquote>
        </div>
    </div>
    </form>
</body>

Now run the project and check the result. 

No comments:

Post a Comment