eWorld.UI - Matt Hawley

Ramblings of Matt

ASP.NET MVC - ActionResult... The Good & Not So Bad

April 18, 2008 18:21 by matthaw

I'm thoroughly enjoying the new ActionResult feature the ASP.NET team introduced into the "refresh" of the Preview 2 bits... or whatever they're calling it now :) Introducing this has increased productivity regarding testing the results of a controller's action. Here's a quick summary of the different types:

  • RenderViewResult - this result is returned when you call RenderView on the controller. It has properties like ViewName, MasterName, ViewEngine, ViewData, and TempData.
  • ActionRedirectResult - this result is returned when you call RedirectToAction on the controller. It exposes a dictionary of information.
  • HttpRedirectResult - this result is returned when you call Redirect. It exposes the Url that it is to redirect to.
  • EmptyResult - This is used for any other purpose in which you don't want the controller to complete the execution on. I have already used this for outputting HTML to the response stream for a specific purpose.

Now that these action results are present, there's no need to have your own view engine that captures the same information. What else is great, is that you can implement your own action result to do whatever you wish!

Onto the Not so Bad

1. When calling RenderView with the parameterless method, this renders testability useless because the RenderViewResult that is returned has Null values where you'd expect to find a value. For example, the critical property "ViewName" is null.

2. I feel that ActionRedirectResult should expose a ViewName and ViewData property. As it stands, when you call RedirectToAction, it takes your view name you've specified and puts it within the RouteValueDictionary it exposes. While this is still testable - it would be much easier to test if I could just go off of ViewName and ViewData.

All in all, this is a very good start to using ActionResults, and I love using them. Hope my insight helps!

kick it on DotNetKicks.com


April 20. 2008 00:56

Pingback from weblogs.asp.net

ASP.NET MVC - ActionResult... The Good & Not So Bad - eWorld.UI - Matt Hawley


April 20. 2008 02:10

Hi Matt, thanks for pointing out the EmptyResult return value, I had not yet discovered that and was coincidentally trying to figure out how to do just that when I ran across this post. One annoyance with it is that you can't just say "return EmptyResult();" like the others; you have to say "return new EmptyResult();". An "EmptyResult" method on the Controller class would be nice to keep it more in line with the other default ActionResults.

Troy Goode

April 20. 2008 02:12

Also, I hope they modify the ComponentController to use ActionResult goodness as well. =)

Troy Goode

May 8. 2008 11:59

Pingback from hsidev.wordpress.com

ASP.NET MVC Resources « HSI Developer Blog


March 21. 2009 11:18

Nice short but very clear post

Bayram Çelik

Comments are closed

Copyright © 2000 - 2024 , Excentrics World