It's just interesting why almost every controller method that I see in the MVC code example returns an ActionResult, even if it is obvious that the code can only return one type of result. I understand that there are certain cases where this is justified, because you can return, say, RedirectResult or ViewResult depending on the logic, but this is not the case for most of the methods that I saw.
Does this not mean that the return type is "object"? Why not just specify JsonResult or FileResult or ViewResult as the return type? Are there any advantages that I do not see to set the return type in ActionResult on each controller method?
Classic example:
public ActionResult Index() { return View(); }
Why does this seem to be the norm instead:
public ViewResult Index() { return View(); }
EDIT: So far, all but one answer indicate that ActionResult is more general. I know so much. :) Why is this accepted practice according to the controller method, and not somewhere else? You are not just returning the highest level classes of the base type that you can use in the usual way, you are trying to return the most specific type that you usually can use. What makes controller methods so different that bloggers and “code writers” (yes, I made that term) just resort to returning an ActionResult?
I quote verbatim the accepted answer here on SO. makes sense to me:
Should ASP.NET MVC Controller methods return an ActionResult?
, , , ActionResult. , ActionResult, .Pro ASP.NET MVC Framework. :" , ViewResult. , ActionResult ( ). , ASP.NET MVC ActionResult, , . - , , ( , ). , , . "
, , , ActionResult. , ActionResult, .
Pro ASP.NET MVC Framework. :
" , ViewResult. , ActionResult ( ). , ASP.NET MVC ActionResult, , . - , , ( , ). , , . "
:
http://www.bengtbe.com/blog/post/2009/07/01/Use-specific-return-types-in-your-ASPNET-MVC-action-methods.aspx
, ActionResult , , .
ActionResult
.
, -, , .
, b/c , . - ( ).
, ActionResult , , , , - , , ...
IMHO - ViewResult, , , , ActionResults.
, ActionResult - , .
ViewResult
System.Object System.Web.Mvc.ActionResult System.Web.Mvc.ViewResultBase System.Web.Mvc.ViewResult
XXXResult . ...
, , MVC ActionResult, . unit test, , .
ScottGu 4; . , ; exmaples, , , - , ( , / b4 ; ). ; HTML JS ( ), " " , JavaScript (, ), . , . , JS- JSON ( ), , , JS- , , . , / , (, ), . , usu casting/coersion, , (, , - , / ). /; ( /benes / /, ). / ( ..), /. , , , ( ). , ? ?