Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must ASP.NET MVC Controller Methods Return ActionResult?

Being new to ASP.NET MVC, I've been wondering about the signature of Controller methods. In all the examples I've seen, they always seem to return ActionResult, even if they actually return a ViewResult instance or similar.

Here's a commonly seen example:

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

In such a case, wouldn't it make more sense to declare the method as public ViewResult Index(), and get stronger type support?

Experimentation indicates that this works, so it seems possible.

I do realize that there may be situations where the polymorphism is desired (e.g. if you want to redirect only in certain situations, but show a view in other situations), but if the method always returns a view, I'd find a ViewResult more desirable.

In terms of future compatibility, ActionResult obviously provides a more robust signature, but if one controls the entire code base, it's always possible to change a method's signature to a more general return type if that should become necessary in the future.

Are the any other considerations that I'm not aware of, or should I just go ahead and declare my controller methods with specific return types?

like image 477
Mark Seemann Avatar asked Jun 20 '09 13:06

Mark Seemann


People also ask

What should I return in ActionResult?

The ActionResult types represent various HTTP status codes. Any non-abstract class deriving from ActionResult qualifies as a valid return type. Some common return types in this category are BadRequestResult (400), NotFoundResult (404), and OkObjectResult (200).

Can I return ActionResult instead of ViewResult?

When you set Action's return type ActionResult , you can return any subtype of it e.g Json,PartialView,View,RedirectToAction.

Why do we use ActionResult in MVC?

The ActionResult method works as a return type of any controller method in the MVC. It acts as the base class of Result classes. It is used to return the models to the Views, file streams, and also redirect to the controllers. It is the responsibility of the Controller that connects the component.


2 Answers

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.

like image 118
BengtBe Avatar answered Sep 20 '22 04:09

BengtBe


Always return the most accurate type you can return. So you should return a ViewResult when the action always shows a view. I would only use ActionResult when you return in ViewResult in some cases (invalid posted data) or a RedirectToRouteResult in other cases.

With some advanced actionfilter/executing scenario's, you can even return totally different things that have nothing to do with ActionResult.

like image 42
Paco Avatar answered Sep 23 '22 04:09

Paco