When to use JsonResult over ActionResult

前端 未结 4 792
时光取名叫无心
时光取名叫无心 2020-12-29 23:57

I\'ve been unable to find a concrete answer regarding this question. I\'ve viewed posts and subsequent posts from this question and elsewhere but all I\'ve really come out o

4条回答
  •  佛祖请我去吃肉
    2020-12-30 00:13

    When to use JsonResult over ActionResult

    I usually return concrete results (e.g. JsonResult, ViewResult) and there are my pros:

    • Controller's interface gives more information about it's behavior. It is easier to work with partial or shared views when you specify PartialViewResult and ViewResult as result type precisely.
    • It is easier to test since it is not needed to cast result to concrete types in your unit tests.

    There are some links where people support this approach:

    • What's the difference between ActionResult and ViewResult for action method?
    • Must ASP.NET MVC Controller Methods Return ActionResult?
    • ViewResult vs ActionResult

    There is a quote from Pro ASP.NET MVC 3 Framework:

    Note Notice that that the return type for the action method in the listing is ViewResult. The method would compile and work just as well if we had specified the more general ActionResult type. In fact, some MVC programmers will define the result of every action method as ActionResult, even when they know it will always return a more specific type. We have been particularly diligent in this practice in the examples that follow to make it clear how you can use each result type, but we tend to be more relaxed in real projects.

    I would use ActionResult over concrete one only if an action should return different types of results. But it is not that common situation.

    I'd like also to add that ActionResult is an abstract class so you cannot simply create an instance of this type and return it. JsonResult is concrete so you can create its instance and return from an action method. There are many other types that derived from ActionResult and basically all of them are used to override ExecuteResult method.

    public abstract class ActionResult
    {
        public abstract void ExecuteResult(ControllerContext context);
    } 
    

    This method is invoked by ControllerActionInvoker and implements logic writing data to response object.

    ControllerActionInvoker does not know about concrete results so that it can handle any result that is derived from ActionResult and implements ExecuteResult.

    In both cases you return an instance of JsonResult type in your example and Json(model) it is simply a Factory Method that creates an instance of JsonResult.

    There is another SO question Is it better to have a method's datatype be as specific as possible or more general? where best practices for method parameters and return values are discussed.

    The general idea is providing abstract types as parameters so that your method can handle wider range of parameter; returning concrete enough types so that your clients do not need to cast or convert them.

提交回复
热议问题