Back to: ASP.NET MVC Tutorial For Beginners and Professionals
Status Results in ASP.NET MVC Application
In this article, I am going to discuss Status Results in ASP.NET MVC Application. Please read our previous article, where we discussed Redirect, RedirectToRoute, and RedirectToAction in the ASP.NET MVC Application. 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. Please read the below articles before proceeding to this article.
We are going to work with the same example that we worked on 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 ate going to discuss the following three things.
- HttpStatusCodeResult
- HttpUnauthorizedResult
- HttpNotFoundResult
HttpStatusCodeResult in ASP.NET MVC:
HttpStatusCodeResult in ASP.NET MVC Framework returns 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 ASP.NET MVC.
using System.Net; using System.Web.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."); } }
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.
Similarly, when we navigate to Home/BadGateway, it will display the following error page.
HttpUnauthorizedResult in ASP.NET MVC
Returning an HttpUnauthorizedResult is the same as returning HttpStatusCodeResult with HttpStatusCode.Unauthorized, it’s just more readable:
using System.Web.Mvc; 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
HttpNotFoundResult in ASP.NET MVC
This is also an overload of HttpStatusCodeResult, but unlike HttpUnauthorizedResult, it actually does have a helper method:
using System.Web.Mvc; 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.
In the next article, I am going to discuss Partial Views in ASP.NET MVC Application. Here, in this article, I try to explain the Status Results in the ASP.NET MVC application with Examples. I hope this Status Results in the ASP.NET MVC article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about these Status Results in the MVC article.