ASP中ViewResult()和ActionResult()的区别是什么?净MVC吗?

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

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

ActionResult是一个抽象类。

ViewResult派生自ActionResult。其他派生类包括JsonResult和PartialViewResult。

这样声明它是为了利用多态性并在同一方法中返回不同类型。

e.g:

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

ViewResult是ActionResult的子类。View方法返回一个ViewResult。这两个代码段做的是完全一样的事情。唯一的区别是,对于ActionResult,你的控制器并不承诺返回一个视图——你可以改变方法体,有条件地返回一个RedirectResult或其他东西,而不改变方法定义。

ActionResult是一个抽象类,它可以有几个子类型。

ActionResult亚型

ViewResult - Renders a specifed view to the response stream PartialViewResult - Renders a specifed partial view to the response stream EmptyResult - An empty response is returned RedirectResult - Performs an HTTP redirection to a specifed URL RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data JsonResult - Serializes a given ViewData object to JSON format JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client ContentResult - Writes content to the response stream without requiring a view FileContentResult - Returns a file to the client FileStreamResult - Returns a file to the client, which is provided by a Stream FilePathResult - Returns a file to the client

资源

动作方法的ActionResult和ViewResult有什么区别?[ASP。网论坛)

出于同样的原因,你不需要编写每个类的每个方法来返回"object"。你应该说得尽可能具体。如果您计划编写单元测试,这一点尤其有价值。不再测试返回类型和/或强制转换结果。

虽然其他答案已经正确地指出了差异,但请注意,如果你实际上只返回ViewResult,最好返回更具体的类型,而不是基本的ActionResult类型。这一原则的一个明显例外是当你的方法返回多个派生自ActionResult的类型时。

关于这一原则背后原因的完整讨论,请参阅这里的相关讨论:必须ASP。NET MVC控制器方法返回ActionResult?

在控制器中,可以使用下面的语法

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

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

在上面的例子中,只有返回类型不同。一个返回ViewResult,另一个返回ActionResult。

ActionResult是一个抽象类。它可以接受:

ViewResult, PartialViewResult, EmptyResult, RedirectResult, RedirectToRouteResult, JsonResult, JavaScriptResult, ContentResult, FileContentResult, FileStreamResult, FilePathResult等。

ViewResult是ActionResult的子类。

在控制器中,我已经用ActionResult指定了下面的代码,这是一个基类,可以在MVC中有11个子类型,如: ViewResult, PartialViewResult, EmptyResult, RedirectResult, RedirectToRouteResult, JsonResult, JavaScriptResult, ContentResult, FileContentResult, FileStreamResult, FilePathResult。

    public ActionResult Index()
                {
                    if (HttpContext.Session["LoggedInUser"] == null)
                    {
                        return RedirectToAction("Login", "Home");
                    }

                    else
                    {
                        return View(); // returns ViewResult
                    }

                }
//More Examples

    [HttpPost]
    public ActionResult Index(string Name)
    {
     ViewBag.Message = "Hello";
     return Redirect("Account/Login"); //returns RedirectResult
    }

    [HttpPost]
    public ActionResult Index(string Name)
    {
    return RedirectToRoute("RouteName"); // returns RedirectToRouteResult
    }

同样地,我们可以使用ActionResult()返回所有这11个子类型,而无需显式地指定每个子类型方法。 如果你要返回不同类型的视图ActionResult是最好的。

为了节省你的时间,这里有一个以前的答案https://forums.asp.net/t/1448398.aspx的链接

ActionResult是一个抽象类,它是ViewResult类的基类。

在MVC框架中,它使用ActionResult类来引用动作方法返回的对象。并对其调用ExecuteResult方法。

ViewResult是这个抽象类的实现。它将尝试通过给定的视图名称在一些预定义的路径(/views/controllername/, /views/shared/等)中找到一个视图页面(通常是aspx页面)。

让你的方法返回一个更具体的类通常是一个很好的实践。如果你确信你的action方法会返回一些视图页面,你可以使用ViewResult。但是如果你的action方法可能有不同的行为,比如呈现视图或执行重定向。您可以使用更通用的基类ActionResult作为返回类型。