Difference Between ViewResult() and ActionResult()

后端 未结 8 1135
忘掉有多难
忘掉有多难 2020-11-30 16:02

What is the difference between ViewResult() and ActionResult() in ASP.NET MVC?

public ViewResult Index()
{
    return View();
}

pu         


        
8条回答
  •  醉酒成梦
    2020-11-30 16:34

    ActionResult is an abstract class.

    ViewResult derives from ActionResult. Other derived classes include JsonResult and PartialViewResult.

    You declare it this way so you can take advantage of polymorphism and return different types in the same method.

    e.g:

    public ActionResult Foo()
    {
       if (someCondition)
         return View(); // returns ViewResult
       else
         return Json(); // returns JsonResult
    }
    

提交回复
热议问题