LA.NET [EN]

Sep 15

In the last post we’ve seen how the ControllerActionInvoker ends up using the ActionResult class returned from your controller methods in order to generate the response that is sent back to the client. Today, we’ll take a  look at the existing ActionResult classes and see what each of them do. Lets start with the base ActionResult class.

public abstract class ActionResult { 
        public abstract void ExecuteResult(ControllerContext context); 
}

As you can see, this is really a very simple abstract class which has only one method called ExecuteRequest. This method receives the current ControllerContext, which means that you’ll be able to access all the the HTTP server side objects and you’ll also be able to get a reference to the current Controller. In the latest release, you’ll have access to the following ActionResult’s derived classes:

model

Lets start with the simple ones…The EmptyResult class doesn’t really do anything. It’s there only to let you specify that a specific action won’t return anything to the user. You can use the HttpUnauthorizedResult to indicate that the current user hasn’t got enough permissions for the current operation (internally, the ExecuteResult method sets the status code of the response to 401).

RedirectResult doesn’t do much too…You can use this action result whenever you need to redirect to another url. Since we’re talking about redirections, there’s also a RedirectToRouteResult. With this class, you’re able to pass a route name and a RouteValueDictionary and it redirect the user to the url calculated from those parameters.

Controller methods don’t always need to get HTML. You can also return JSON from your controller’s methods. The JsonResult class is there and will help you do that. Internally, it uses the JavaScriptSerializer and it serializes the object that is passed to its Data property.

You can also return text from a controller method by using the ContentResult class. This class will write the specified text to the output and you can also set the encoding and content type of the current response. Until now, we’ve assumed that all action methods returned ActionResult instances (in fact, I’ve even said that in one of the previous post), but the truth is that you can also return another type. When that happens,the instance returned from your object is wrapped in a ContentResult action,which is then responsible for converting it to a string (by calling the Convert.ToString method) and pushing it through the response stream. Btw, it’s also interesting to point out that if your method returns void, you will end up using the EmptyResult action.

Finally, you’ve also got the PartialViewResult class. There’s also a ViewResult class, that extends the PartialViewResult class. Both these classes will let you render views that return HTML to the client. I won’t be spending more time on these classes becaus the next post will be about them. The base Controller class exposes several methods that encapsulate the creation of these types of action results. Currently, you have access to these:

  • ContentResult: responsible for creating an action of type ContentResult;
  • JsonResult: as you might expect, this method will return an instance of the JsonResult type;
  • PartialViewResult: used for partial rendering. Returns an instance of type PartialViewResult;
  • RedirectResult: used for returning an object of type RedirectResult that will redirect the user to a new url;
  • RedirectToRouteResult: responsible for creating an instance of type RedirectToRouteResult;
  • ViewResult: returns an instance of the ViewResult object which ends up rendering a page.

So, when you build your own controller methods, you can create a new instance of the ActionResult you want or invoke the method with the some name and pass it the correct parameters for the current scenario. As I’ve said, on the next post we’ll take a closer look at the PartialViewResult and ViewResult classes. Keep tuned!

1 comment so far

  1. ddog
    9:14 pm - 6-26-2009

    please use a better font!!

Leave a Reply

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


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>