Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does most MVC sample controller code return ActionResult?

Tags:

asp.net-mvc

Just wondering why almost every controller method I see in sample MVC code returns ActionResult, even if it's obvious that the code can only return one type of result. I understand there are certain instances where it's warranted because you may return, say a RedirectResult or a ViewResult depending on the logic, but that's not the case for most of the methods I've seen.

Isn't it tantamount to having a return type of 'object' on a method? Why not just specify JsonResult, or FileResult or ViewResult as the return type? Is there a benefit I'm not seeing to settting the return type to ActionResult on every controller method?

Classic example:

public ActionResult Index()
{
    return View();
}

Why does this seem to be the norm instead of this:

public ViewResult Index()
{
    return View();
}

EDIT: So far all of the responses except for one have indicated that ActionResult is just more generic. I know that much. :) Why is this accepted practice on a controller method though, and not anywhere else? You don't just return the highest level-base classes of a type that you can on a normal method, you attempt to return the most specific type you can usually. What makes controller methods so different that bloggers and "sample code writers" (yes, I made that term up) would just resort to returning ActionResult?

like image 277
Scott Avatar asked Jan 06 '11 16:01

Scott


2 Answers

quoted verbatim from an accepted answer here on SO. makes sense to me:

Must ASP.NET MVC Controller Methods Return ActionResult?

You can absolutely use specific return types, even though most examples on the web seems to return the ActionResult. The only time I would return the ActionResult class is when different paths of the action method returns different subtypes.

Steven Sanderson also recommends returning specific types in his book Pro ASP.NET MVC Framework. Take a look at the quote below:

"This action method specifically declares that it returns an instance of ViewResult. It would work just the same if instead the method return type was ActionResult (the base class for all action results). In fact, some ASP.NET MVC programmers declare all their action methods as returning a nonspecific ActionResult, even if they know for sure that it will always return one particular subclass. However, it's a well-established principle in object-oriented programming that methods should return the most specific type they can (as well as accepting the most general parameter types they can). Following this principle maximizes convenience and flexibility for code that calls your method, such as your unit tests."

see also:

http://www.bengtbe.com/blog/post/2009/07/01/Use-specific-return-types-in-your-ASPNET-MVC-action-methods.aspx

like image 153
jim tollan Avatar answered Oct 05 '22 13:10

jim tollan


My guess would be that ActionResult gets returned rather than a more specific result simply because there's no need to make the code more specific.

Using a more generic type keeps things more flexible.

Keep in mind that changing the return type may not be an issue in the Web Application project, but it would also cause you to change all your Unit Tests in the test project as well.

like image 31
Justin Niessner Avatar answered Oct 05 '22 13:10

Justin Niessner