Thursday, July 10, 2014

ActionResult Return Type in MVC 3.0

Introduction

An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method. 

There are many derived ActionResult types in MVC that you may use to return the results of a controller method; these are more specific for a particular view. All Action Result Classes are derived from "ActionResult". In other words ActionResult is an abstract class that has several subtypes.

ViewResult

It renders a specified view to the response stream. The "ViewResult" class inherits from the abstract class "ViewResultBase" and is used to render a view. This class contains methods for finding the view to render and also for executing the result. This class also contains properties that identify the view (view Name) to render for the application.
public ViewResult ViewResultTest()
{
    return View("ViewResultTest");
}

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.view.aspx

PartialViewResult

The PartialViewResult class is inherited from the abstract "ViewResultBase" class and is used to render a partial view. This class contains methods for finding the partial view to render and also for executing the result. This class also contains properties that identify the partial view (View Name) to render for the application. PartialViews are not common as action results. PartialViews are not the primary thing being displayed to the user, which is the View. The partial view is usually a widget or something else on the page.
public PartialViewResult PartialViewResultTest()
{
    return PartialView("PartialViewResult");
}


RedirectResult

It is used to perform an HTTP redirect to a given URL. This class is inherited from the "ActionResult" abstract class.
public ActionResult RedirectResultTest()
{
    return Redirect("http://www.google.com/");
public RedirectResult RedirectResultTest()
{
    return Redirect("http://www.google.com/");
}

http://msdn.microsoft.com/en-us/library/system.web.mvc.redirectresult.aspx
RedirectToRouteResult

RedirectToResult is used to redirect by using the specified route values dictionary. This class is inherited from the "ActionResult" abstract class.
public ActionResult RedirectToRouteResultTest(int? Id)
{
    return new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { controller = "Home", action = "List", Id = new int?() }));
}

http://msdn.microsoft.com/en-us/library/system.web.mvc.redirecttorouteresult.aspx

ContentResult

The ContentResult may be used to return to an action as plain text. This class is inherited from the "ActionResult" abstract class. 
public ActionResult ContentResultTest()
{
    return Content("Hello My Friend!");
public ContentResult ContentResultTest()
{
    return Content("Hello My Friend!");
}

JsonResult

Action methods on controllers return JsonResult (JavaScript Object Notation result) that can be used in an AJAX application. This class is inherited from the "ActionResult" abstract class. Here Json is provided one argument which must be serializable. The JSON result object that serializes the specified object to JSON format.
public JsonResult JsonResultTest()
{
    return Json("Hello My Friend!");
}

http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult(v=vs.98).aspx

No comments:

Post a Comment