Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to explicitly specify the type of ActionResult returned by a controller in ASP.NET MVC

Tags:

asp.net-mvc

I've been using ASP.NET MVC for a little while now and seem to find myself constantly returning things other than ActionResult from my controllers. I obviously return ViewResults but also JSonResults and also a couple of custom results that we've built in house.

I'm wondering tho, if, instead of declaring my controller methods like:

public ActionResult Index()

I should start declaring them as

public ViewResult Index()

or

public JsonResult Search()

if I always know that the Index action on my controller will always return a ViewResult or the Search action on my controller will always return a JsonResult?

EDIT: Just to clarify, I'm talking specifically about situations where I will always want a specific type of ActionResult to be returned.

like image 433
lomaxx Avatar asked Jul 09 '09 01:07

lomaxx


3 Answers

I vote yes for two reasons.

  1. You are explicitly declaring what you expect the method to return and the compiler will catch any attempt to do otherwise. No need for a unit test that does a Assert( result is ViewResult).

  2. You have to cast the result to the expected type in your tests when examining any properties unique to that result type (For example, checking Url property of the RedirectResult). Simply declaring the test variable as var removes any brittleness incurred by changing types.

like image 160
Paul Alexander Avatar answered Oct 08 '22 01:10

Paul Alexander


By declaring a more specific return type you're gaining a little bit more of compiler type checking that you now don't have to cover in unit tests.

You would be binding yourself, however, to that type, and would have to revert if that changes. A common example is if you have to redirect the user elsewhere on some special conditions by returning a RedirectResult.

like image 40
Oren Trutner Avatar answered Oct 08 '22 00:10

Oren Trutner


I would leave it as the generic ActionResult. If you make it the specific result and change it later some, if not all, of your unit tests will need to be rewritten to accommodate the change. This will make your unit tests more brittle than they need to be.

EDIT: additionally, by leaving it as an ActionResult, you allow yourself the ability to return various different results based on you action logic. For example, the normal flow of your method might return a RedirectResult, but you may have error paths that return a ViewResult or HttpUnauthorizedResult. If you type your method more strongly than needed originally, you may end up having to unnecessarily refactor it and your tests as you add in alternative results later.

The bottom line is that I don't see any real advantages and, at least a couple, disadvantages.

like image 35
tvanfosson Avatar answered Oct 08 '22 01:10

tvanfosson