ActionResult types in MVC2 - Raj Kaimal

ActionResult types in MVC2

In ASP.NET MVC, incoming browser requests gets mapped to a controller action method. The action method returns a type of ActionResult in response to the browser request. A basic example is shown below:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

Here we have an action method called Index that returns an ActionResult. Inside the method we call the View() method on the base Controller. The View() method, as you will see shortly, is a method that returns a ViewResult.

The ActionResult class is the base class for different controller results. The following diagram shows the types derived from the ActionResult type.

image

ASP.NET has a description of these methods

  • ContentResult – Represents a text result.
  • EmptyResult – Represents no result.
  • FileContentResult – Represents a downloadable file (with the binary content).
  • FilePathResult – Represents a downloadable file (with a path).
  • FileStreamResult – Represents a downloadable file (with a file stream).
  • JavaScriptResult – Represents a JavaScript script.
  • JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  • PartialViewResult – Represents HTML and markup rendered by a partial view.
  • RedirectResult – Represents a redirection to a new URL.
  • RedirectToRouteResult – Represents a result that performs a redirection by using the specified route values dictionary.
  • ViewResult – Represents HTML and markup rendered by a view.

To return the types shown above, you call methods that are available in the Controller base class. Instead of creating instances of ActionResult or it’s inherited type, you call the helper methods on the Controller class and return it. A list of these methods are shown below.
image 
The helper methods on the controller base class and the ActionResult type they return are listed here (ref: http://msdn.microsoft.com/en-us/library/dd410269.aspx):

Action Result

Helper Method

Description

ViewResult

View

Renders a view as a Web page.

PartialViewResult

PartialView

Renders a partial view, which defines a section of a view that can be rendered inside another view.

RedirectResult

Redirect

Redirects to another action method by using its URL.

RedirectToRouteResult

RedirectToAction

RedirectToRoute

Redirects to another action method.

ContentResult

Content

Returns a user-defined content type.

JsonResult

Json

Returns a serialized JSON object.

JavaScriptResult

JavaScript

Returns a script that can be executed on the client.

FileResult

File

Returns binary output to write to the response.

EmptyResult

(None)

Represents a return value that is used if the action method must return a null result (void).

Methods without an ActionResult return type
The MVC framework will translate action methods that do not return an ActionResult into one. Consider the HomeController below which has methods that do not return any ActionResult types. The methods defined return an int, object and void respectfully.

public class HomeController : Controller
{
    public int Add(int x, int y)
    {
        return x + y;
    }
 
    public Employee GetEmployee()
    {
        return new Employee();
    }
 
    public void DoNothing()
    {
        
    }
}

When a request comes in, the Controller class hands internally uses a ControllerActionInvoker class which inspects the action parameters and invokes the correct action method. The CreateActionResult method in the ControllerActionInvoker class is used to return an ActionResult. This method is shown below. If the result of the action method is null, an EmptyResult instance is returned. If the result is not of type ActionResult, the result is converted to a string and returned as a ContentResult.

protected virtual ActionResult CreateActionResult(ControllerContext controllerContext, ActionDescriptor actionDescriptor, object actionReturnValue) {
    if (actionReturnValue == null) {
        return new EmptyResult();
    }
 
    ActionResult actionResult = (actionReturnValue as ActionResult) ??
        new ContentResult { Content = Convert.ToString(actionReturnValue, CultureInfo.InvariantCulture) };
    return actionResult;
}

 

In the HomeController class above,

  • the DoNothing method will return an instance of the EmptyResult()
    • Renders an empty webpage
  • the GetEmployee() method will return a ContentResult which contains a string that represents the current object
    • Renders the text “MyNameSpace.Controllers.Employee” without quotes.
  • the Add method for a request of /home/add?x=3&y=5 returns a ContentResult
    • Renders the text “8” without quotes.


Unit Testing

The nice thing about the ActionResult types is in unit testing the controller. We can, without starting a web server, create an instance of the Controller, call the methods and verify that the type returned is the expected ActionResult type. We can then inspect the returned type properties and confirm that it contains the expected values.

Enjoy!

Sulley: Hey, Mike, this might sound crazy but I don't think that kid's dangerous.
Mike: Really? Well, in that case, let's keep it. I always wanted a pet that could kill me.

Published Monday, May 3, 2010 1:24 AM by rajbk
Filed under: , ,

Comments

# re: ActionResult types in MVC2

Very usefull article, thanks !

Monday, May 3, 2010 2:55 PM by Chouteau

# re: ActionResult types in MVC2

Haven't used JavaScriptResult before as I tend to return a JsonResult back and then execute as necessary on the client but very useful to know what the options are.  Thanks for the diagram!

James

Thursday, October 14, 2010 3:45 AM by James Radford