Must ASP.NET MVC Controller Methods Return ActionResult?

前端 未结 6 664
情书的邮戳
情书的邮戳 2020-12-02 14:24

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 th

相关标签:
6条回答
  • 2020-12-02 14:43

    [Partial answer]: You don't always return ActionResult, no. Here's a quick view of some other results you can return: http://msdn.microsoft.com/en-us/library/dd410269%28v=vs.98%29.aspx

    Maybe that'll help a little. Good luck!

    0 讨论(0)
  • 2020-12-02 14:47

    Yep I have Sanderson's book, and I liked that part about being specific, as that was something vexing me when I was looking at other controller action examples. My philosophy even b4 learning MVC was that since functions (methods that return a value) should be treated as if you were declaring a variable/be substitutable in context for a variable/ref of the same type, be specific about the type, as you would if declaring a var (think of it as wanting to avoiding defnining all your variables as type "Object" in an app - more robust, but you lose some design-time checking and type safety). Facilitates a controller unit test for the correct return type as well.

    For related reference, check out Listkov's Substitution Principle (the "L" in "SOLID") also.

    0 讨论(0)
  • 2020-12-02 14:50

    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.

    0 讨论(0)
  • 2020-12-02 14:53

    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.

    0 讨论(0)
  • 2020-12-02 14:55

    Yes, you can define your action like: public ViewResult Index(). But sometimes your action can return different results (it is impossible without declaring result as base ActionResult class). For example:

    public ActionResult Show()
    {
        ...
    
        if(Request.IsAjaxRequest())
        {
            return PartialView(...);
        }
    
        return View(...);
    }
    

    or:

    public ActionResult Show()
    {
        ...
    
        try
        {
            ...
        }
        catch(Exception)
        {
            return RedirectToAction(...);
        }
    
        return View(...);
    }
    
    0 讨论(0)
  • 2020-12-02 14:55

    ActionResult is the base class for the various return types. So your action must return an ActionResult or a class derived from it in order to work. Common ones are ViewResult, JsonResult, etc.

    0 讨论(0)
提交回复
热议问题