ASP.NET MVC’s ActionResult in depth

by Milad 22. February 2010 20:47

ActionResult Class is a valuable abstract class in ASP.NET MVC Framework. ActionMethods mostly return an ActionResult.

First let’s see what is an ActionMethod ?

Any class that inherits from System.Web.Mvc.Controller can use action methods to
serve web requests. The requirements for a method to be web-callable as an action method are well documented on http://www.asp.net/mvc. The method
■ Must be public
■ Cannot be a static method
■ Cannot be an extension method
■ Cannot be a constructor, getter, or setter
■ Cannot have open generic types
■ Is not a method of the Controller base class
■ Is not a method of the ControllerBase base class
■ Cannot contain ref or out parameters *

There are some other considerations about ActionMethods like they should not perform data access etc. But this post is about ActionResult class.

According to Reflector, ActionResult’s base type is System.Object and there are 14 classes that are Inherited from this class.

1. ContentResult  —Represents a text result
2. EmptyResult  —Represents no result
3. FileResult  —Represents a downloadable file (abstract class)
4. FileContentResult  —Represents a downloadable file (with the binary content)
5. FilePathResult  —Represents a downloadable file (with a path)
6. FileStreamResult  —Represents a downloadable file (with a file stream)
7. HttpUnauthorizedResult  —Represents the result of an unauthorized  HTTP
request
8. JavaScriptResult  —Represents a JavaScript script
9. JsonResult  —Represents a JavaScript Object Notation (JSON) result that can
be used in an AJAX application
10. RedirectResult  —Represents a redirection to a new URL
11. RedirectToRouteResult  —Represents a result that performs a redirection
given a route values dictionary
12. PartialViewResult  —Base class used to send a partial view to the response
13. ViewResult  —Represents HTML and markup
14. ViewResultBase  —Base class used to supply the model to the view and then render the view to the response

The important point of ASP.NET MVC Framework is it’s Extendibility. So the question here is how can we create a new ActionResult? The answer is quite simple!

namespace MyActionResults
{
    public class SimpleActionResult : System.Web.Mvc.ActionResult
    {
        private string msg;
        public SimpleActionResult(string Message)
        {
            msg = Message;
        }

        public override void ExecuteResult(System.Web.Mvc.ControllerContext context)
        {
            context.HttpContext.Response.Write(msg);
        }
    }
}

This ActionResult, prints a simple message to it’s HTTPContext(Like ContentResult). You can do so many interesting things with this class. Like in MvcContrib Library, There is an XMLResult Class, that puts an XML Output into the response stream.

P.S: This post is mostly inspired by Chapter 4 of Manning ASP.NET MVC in Action Book, titled The controller in depth. I highly recommend this book. :-)

Tags: , , , ,

Comments

6/6/2010 7:25:43 PM #

playstation 3 controller review

Just now I discovered the web for exactly this kind of information. Thank to your post that search has to end right now. You wrote the post in a very understandable way. With that, I added your blogs as one of my favorites! Cheers!

playstation 3 controller review United States |

6/7/2010 6:42:20 PM #

Web Marketing

Exceptionally unexpected.

Web Marketing United States |

6/10/2010 4:16:42 AM #

abercrombie and fitch

Very habitually I go to see this internet site. It very much is pleasant to me. Thanks the author

abercrombie and fitch United States |

6/11/2010 4:21:26 AM #

play games

Your blog is  getting more traffic thanks to sites like Youtube and other social sites. Thanks for the awesome article I’ll be back for updates.

play games United States |

6/16/2010 6:57:10 AM #

Carman Weeks

Awesome article, numerous wonderful info.  I'm going to show my friend and ask them the things they think.

Carman Weeks United States |