ActionResult Helper Methods in ASP.NET MVC

by Michal Rogozinski 27. July 2009 16:24

Just wanted to quickly summarize default ActionResult methods in ASP.NET MVC Framework. I’m looking for them so often that I just need them all to be in one place.

Redirect(…) Returns a RedirectResult, which redirects the user to the appropriate URL;
RedirectToAction(…) Returns a RedirectToRouteResult, which redirects the user to an action using the supplied route values;
RedirectToRoute(…) Returns a RedirectToRouteResult, which redirects the user to the URL that matches the specified route values;
View(…) Returns a ViewResult, which renders the View to the response;
PartialView(…) Returns a PartialViewResult, which writes the specified content (string) to the response;
Content(…) Returns a ContentResult, which writes the specified content (string) to the response;
File(…) Returns a class that derives from FileResult, which writes binary content to the response;
Json(…) Returns a ContentResult containing the output from serializing an object to JSON;
JavaScript(…) Returns a JavaScriptResult containing JavaScript code that will be immediately executed when returned to the client;

Returning ActionResult object has its advantages over returning simple types or tampering with the Response object. First of all it makes your intentions slightly more clear. It is also easier to unit test.

Also bear in mind that there are implicit conversions when returning a non-ActionResult types:

  • Action invoker replaces null results with an instance of EmptyResult. This follows the Null Object Pattern.
  • Action invoker treats void the same way as if it returned null, and thus an EmptyResult is returned.
  • For Object (anything other than ActionResult) the action invoker call ToString using InvariantCulture on the object and wraps the resulting string in a ContentResult instance.

Tags:

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Michal Rogozinski

Michal Rogozinski

 

 

Contact me via email  E-mail    My status  Skype me

My ScheduleFree/Busy schedule

 

Profile for microg