Difference Between ViewResult() and ActionResult()

后端 未结 8 956
忘掉有多难
忘掉有多难 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:48

    ViewResult is a subclass of ActionResult. The View method returns a ViewResult. So really these two code snippets do the exact same thing. The only difference is that with the ActionResult one, your controller isn't promising to return a view - you could change the method body to conditionally return a RedirectResult or something else without changing the method definition.

    0 讨论(0)
  • 2020-11-30 16:49

    In the Controller , one could use the below syntax

    public ViewResult EditEmployee() {
        return View();
    }
    
    public ActionResult EditEmployee() {
        return View();
    }
    

    In the above example , only the return type varies . one returns ViewResult whereas the other one returns ActionResult.

    ActionResult is an abstract class . It can accept:

    ViewResult , PartialViewResult, EmptyResult , RedirectResult , RedirectToRouteResult , JsonResult , JavaScriptResult , ContentResult, FileContentResult , FileStreamResult , FilePathResult etc.

    The ViewResult is a subclass of ActionResult.

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