Handling "Internal Server Error" and "Page Not Found" errors gracefully in an ASP.NET application involves implementing custom error pages and proper error logging. Here’s a detailed approach for handling these errors step to step .

Step 1-  Add two View on Controller :
  • For Error Handling
  • For Page Not Found

HomeController Code :

            public ActionResult Error()
            {
                return View();
            }

            public ActionResult NotFound()
            {
                return View();
            }


Step 2 : Designed both pages  as you want , for example : 
Error.cshtml Code :

    @{
        ViewBag.Title = "Error";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    <section class="breadcrumbs">
        <div class="container">
            <ol>
                <li><a href="@Url.Action("Index", "Home")">Home</a></li>
            </ol>
        </div>
    </section>
    <div class="section-title container">
        <h3><span>Oops! Something went wrong.</span></h3><br/>
<a style="text-decoration:underline" href="@Url.Action("Index","Home")">Back To Home
<i class="fa fa-home"></i></a>
        <h3><span>
            <marquee behavior="alternate">We are working on fixing the issue. Please try again later.</marquee>
        </span></h3>
    </div>



NotFound.cshtml Code :

    @{
        ViewBag.Title = "Not Found";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    <section class="breadcrumbs">
        <div class="container">
            <ol>
                <li><a href="@Url.Action("Index", "Home")">Home</a></li>
            </ol>
        </div>
    </section>
    <div class="section-title container">
        <h3><span>404 - Page Not Found</span></h3><br/>
<a style="text-decoration:underline" href="@Url.Action("Index","Home")">Back To Home
<i class="fa fa-home"></i></a>
        <h3><span>
            <marquee behavior="alternate">Sorry, the page you are looking for is not available.</marquee>
        </span></h3>
    </div>




Step 3 -  (For ASP.NET MVC: )
Go to your Global.asax.cs And Create an method to handle Error as below :
Override the Application_Error method in Global.asax.cs to handle exceptions globally:
Global.asax.cs Code :

    public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                //BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
            protected void Application_Error()
            {
                Exception exception = Server.GetLastError();

                Server.ClearError();
                Response.Redirect("~/Home/Error");
            }
        }

Step 4 : Go to your web.config file and add inside <system.web></system.web> as add as below :

 <system.web>
 <!-- Code for Error Handling start-->
    <customErrors mode="Off" defaultRedirect="~/Home/Error">
      <error statusCode="404" redirect="~/Home/NotMatch"/>
      <error statusCode="500" redirect="~/Home/Error"/>
    </customErrors>
    <!-- Code for Error Handling end-->
    <httpRuntime targetFramework="4.5" />

    <compilation debug="true" targetFramework="4.5" />
   
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>

Leave a Reply

Your email address will not be published. Required fields are marked *


Talk to us?

Post your blog

F.A.Q

Frequently Asked Questions