Shravan Kumar Kasagoni

Skip Navigation
New ActionResult Types in ASP.NET MVC3

New ActionResult Types in ASP.NET MVC3

 /  Leave a response

ASP.NET MVC3 includes 3 new “ActionResult” Types.

HttpNotFoundResult: Instance of HttpNotFoundResult class indicates to client(browser) that the requested resource was not found. It returns a 404 HTTP status code to the client. Generally we return 404 status code if a requested webpage is not available. In case of MVC applications we 404 status code is in terms of resources, for example we are searching for particular user profile in the portal, if the user profile is not found, we can return 404.

How to return 404 status code from a MVC application?

First way is to instantiate HttpNotFoundResult class and return the object.

public ActionResult Index()
{
    var result = new HttpNotFoundResult();
    return result;
}

Next alternative is to makes use of HttpNotFound() helper method of the Controller class which returns the HttpNotFoundResult instance.

public ActionResult Index()
{
    return HttpNotFound();
}

We can return 404 along with custom status code description,using the overloaded version of HttpNotFound(string statusDescription).

public ActionResult Index()
{
    return HttpNotFound("can not find the requested resource");
}

RedirectResult: RedirectResult class is already available in ASP.NET MVC 2, which indicates a temporary redirect (HTTP 302 status code). In ASP.NET MVC 3 RedirectResult class includes a new boolean property “Permanent” which indicates a permanent redirect (HTTP 301 status code).

The Controller class now has three new methods for performing permanent redirects,These methods return an instance of RedirectResult with the Permanent property set to true.

  • RedirectPermanent()
  • RedirectToRoutePermanent()
  • RedirectToActionPermanent()
using System.Web.Mvc;
namespace RedirectPermanent.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return RedirectPermanent("http://65.1.162.244");
        }
        public ActionResult RedirectToNewRoute()
        {
            return RedirectToRoutePermanent("NewRoute");
        }
        public ActionResult RedirectToNewAction()
        {
            return RedirectToActionPermanent("Index","NewHome");
        }
    }
}

RedirectResult is very helpful for the SEO (search engine optimization) , which indicates to the web crawlers to update the existing links in  search engine  if in case a resource or URL is moved permanently to new resource or URL.

HttpStatusCodeResult: HttpStatusCodeResult class is Returns a user-specified HTTP status code. We can use the instance of HttpStatusCodeResult to return the any http status code and along with a custom description. for example here I am return the 401 status code, which specifies The request requires user authentication.

public ActionResult Index()
{
    var errorMsg = "you are unauthorized to acess this resource";
    return new HttpStatusCodeResult(401, errorMsg);
}

Write a response

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

All code will be displayed literally.