Error Pages Based on Status Code in ASP.NET MVC

Error Pages Based on Status Code in ASP.NET MVC

In this article, I am going to discuss how to display different Error Pages Based on Status Code in the ASP.NET MVC application. Please read our previous two articles as this is a continuation article to our previous articles. In our previous two articles, we discussed Exception Filters in MVC and how to create and use Custom Exception Filter in ASP.NET MVC applications.

Creating Error Controller:

Create a controller with the name ErrorController within the Controllers folders. Once you create the Error Controller then copy and paste the following code in it.

using System.Web.Mvc;
namespace ExceptionFilterInMVC.Controllers
{
    public class ErrorController : Controller
    {
        public ActionResult PageNotFoundError()
        {
            return View();
        }

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

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

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

Note: If you have any specific status code other than the above, then you can also create that specific status code action method.

Creating Error Views:

Now, we need to create the respective error views.

PageNotFoundError.cshtml
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>PageNotFound Error</title>
</head>
<body>
    <hgroup>
        <h1>Page Not Found Error</h1>
        <h2>The Page you are trying to access is no longer available. Kindly check and submit the URL again</h2>
    </hgroup>
</body>
</html>
UnauthorizedError.cshtml
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Unauthorized Error</title>
</head>
<body>
    <hgroup>
        <h1>Unauthorized Error</h1>
        <h2>You donot have the permission to access this page. Kindly contact with your admin.</h2>
    </hgroup>
</body>
</html>
InternalServerError.cshtml
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Internal Server Error</title>
</head>
<body>
    <hgroup>
        <h1>Internal Server Error</h1>
        <h2>Some Internal Server error Occurred while processing your request. Kindly try after some time.</h2>
    </hgroup>
</body>
</html>
GenericError.cshtml
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Generic Error</title>
</head>
<body>
    <hgroup>
        <h1>Generic Error</h1>
        <h2>An unknown error has occurred. We are working on it. Please try after some time</h2>
    </hgroup>
</body>
</html>
Registering the Error Views based on Status Code:

Our requirement is to register the respective error pages based on their status code. If the error status code is not available then we need to display the Generic Error page to the end-user. To do this, open the web.config file and modify the following CustomError elements within the System.Web as shown below.

<customErrors mode="On" defaultRedirect="~/Error/GenericError">
  <error statusCode="404" redirect="~/Error/PageNotFoundError"/>
  <error statusCode="500" redirect="~/Error/InternalServerError"/>
  <error statusCode="401" redirect="~/Error/UnauthorizeddError"/>
</customErrors>

That’s it. Now run the application and navigates to a URL that does not exist and you will see that 404 custom error page will display. I hope now you understood how to display different error pages based on the Status code in ASP.NET MVC Application. In the next article, I am going to discuss the Child Action Only Attribute in ASP.NET MVC Application with one real-time example.

Leave a Reply

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