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

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

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

当前回答

在控制器中,我已经用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是最好的。

其他回答

在控制器中,我已经用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是最好的。

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

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的子类。

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

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

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

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。网论坛)