Status Result in MVC

Status Result in ASP.NET MVC

ASP.NET MVC has different types of Action Results. Each action result returns a different format of the output. As a programmer, we need to use different action results to get the expected output. Action Results return the result to view the page for the given request. In this article, I am going to discuss Status Result in MVC application.

Please read below articles before proceeding to this article.

Action Result Overview in ASP.NET MVC

View Result and Partial View Result in ASP.NET MVC.

File Result, Content Result, Empty Result, JavaScript Result and JSON Result in MVC.

Redirect, RedirectToRoute, and RedirectToAction Result in MVC.

We are going to work with the same example that we started in our previous 4 articles.

The Status Action Results will return status codes to the browser for it to use. As part of the Status Result, we will discuss the following three things.

  1. HttpStatusCodeResult
  2. HttpUnauthorizedResult
  3. HttpNotFoundResult

HttpStatusCodeResult:

HttpStatusCodeResult return an HTTP status code to the browser, along with a custom message to be displayed:

Let’s modify the Home Controller to understand this concept in MVC.

public class HomeController : Controller
{
    public HttpStatusCodeResult UnauthorizedStatusCode()
    {
        return new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "You are not authorized to access this controller action.");
    }

    public HttpStatusCodeResult BadGateway()
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadGateway, "I have no idea what this error means.");
    }
}

Notice that there is no helper method for this ActionResult.

The HttpStatusCode enumeration contains all HTTP status codes (so that you don’t have to remember what 402 or 307 mean). These are useful in exception-driven scenarios where you have custom error pages defined.

So when we navigate to Home/UnauthorizedStatusCode, it will display the following error page

Status Result in ASP.NET MVC

Similarly, when we navigate to Home/BadGateway, it will display the following error page

Status Result in ASP.NET MVC

HttpUnauthorizedResult

Returning an HttpUnauthorizedResult is the same as returning HttpStatusCodeResult with HttpStatusCode.Unauthorized, it’s just more readable:

public class HomeController : Controller
{
    public HttpStatusCodeResult UnauthorizedResult()
    {
        return new HttpUnauthorizedResult("You are not authorized to access this controller action.");
    }
}

When we navigate to Home/UnauthorizedResult, then it will display the following error page

Status Result in ASP.NET MVC

HttpNotFoundResult

This is also an overload of HttpStatusCodeResult, but unlike HttpUnauthorizedResult, it actually does have a helper method:

public class HomeController : Controller
{
    public HttpNotFoundResult NotFound()
    {
        return HttpNotFound("We didn't find that action, sorry!");
    }
}

When we navigate to Home/NotFound, then it will display the following error page

Status Result in ASP.NET MVC

SUMMARY

In this article, I try to explain the Status Result in ASP.NET MVC application step by step with a simple example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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