ActionResult types in MVC2

Posted by rajbk on ASP.net Weblogs See other posts from ASP.net Weblogs or by rajbk
Published on Mon, 03 May 2010 06:24:55 GMT Indexed on 2010/05/03 6:28 UTC
Read the original article Hit count: 1333

Filed under:
|
|

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. A list of these methods are shown below.
image 

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.

© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about ASP.NET