HttpStatusCodeResult In ASP.NET MVC

Introduction

The Status Action Results will return the status codes to the browser. As part of the Status Result, I will discuss the following three status code.

  1. HttpStatusCodeResult
  2. HttpUnauthorizedResult
  3. HttpNotFoundResult

HttpStatusCodeResult

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

HttpUnauthorizedResult

HttpUnauthorizedResult is the same as HttpStatusCodeResult HttpStatusCode.Unauthorized. It does not allow unauthorized users.

HttpNotFoundResult

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

The HttpStatusCode enumeration contains all HTTP status codes (so that you don’t have to remember what 402 or 307 means). These are useful in exception-driven scenarios where you have custom error pages defined. There is no helper method for these ActionResult.

Step 1

Open Visual Studio 2015 or your choice and create a new project.

Step 2

Choose web application project and give an appropriate name for your project.
 
HttpStatusCodeResult In ASP.NET MVC 

Step 3

Select an empty template, check the MVC checkbox below, and click OK.
 
HttpStatusCodeResult In ASP.NET MVC 

Step 4

Right-click the Controllers folder and add a controller.
 
HttpStatusCodeResult In ASP.NET MVC 

A window will appear. Choose MVC5 Controller-Empty and click "Add".

HttpStatusCodeResult In ASP.NET MVC 

After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home.

HttpStatusCodeResult In ASP.NET MVC 

Here is the complete code for Home Controller

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7.    
  8. namespace MvcStatusResult_Demo.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.    
  13.         public HttpStatusCodeResult BadGateway()  
  14.         {  
  15.             return new HttpStatusCodeResult(HttpStatusCode.BadGateway,"There is something wrong. please contact admin.");  
  16.         }  
  17.    
  18.         public HttpStatusCodeResult Unauthorized()  
  19.         {  
  20.             return new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "You are not authorized to access this controller action.");  
  21.         }  
  22.    
  23.         public HttpStatusCodeResult NotFound()  
  24.         {  
  25.             return HttpNotFound("Your request did not find.");  
  26.         }  
  27.     }  
  28. }  

Step 5

Build your project and press ctrl+5 to run the project

http://localhost:61788/home/BadGateway

HttpStatusCodeResult In ASP.NET MVC 

http://localhost:61788/home/Unauthorized

HttpStatusCodeResult In ASP.NET MVC 

http://localhost:61788/home/NotFound

HttpStatusCodeResult In ASP.NET MVC 

Summary

In this article, I tried to explain the Status Result in ASP.NET MVC applications step by step with a simple example. I hope this article will help you.


Similar Articles