ViewResult()和ActionResult()之间的区别

本文翻译自:Difference Between ViewResult() and ActionResult()

What is the difference between ViewResult() and ActionResult() in ASP.NET MVC? ASP.NET MVC中的ViewResult()ActionResult()什么区别?

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

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

#1楼

参考:https://stackoom.com/question/Ju3x/ViewResult-和ActionResult-之间的区别


#2楼

In the Controller , one could use the below syntax 在Controller中,可以使用以下语法

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 . 一个返回ViewResult而另一个返回ActionResult

ActionResult is an abstract class . ActionResult是一个抽象类。 It can accept: 它可以接受:

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

The ViewResult is a subclass of ActionResult . ViewResultActionResult的子类。


#3楼

ActionResult is an abstract class. ActionResult是一个抽象类。

ViewResult derives from ActionResult . ViewResult派生自ActionResult Other derived classes include JsonResult and PartialViewResult . 其他派生类包括JsonResultPartialViewResult

You declare it this way so you can take advantage of polymorphism and return different types in the same method. 您以这种方式声明它,以便您可以利用多态并在同一方法中返回不同的类型。

eg: 例如:

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

#4楼

ViewResult is a subclass of ActionResult. ViewResult是ActionResult的子类。 The View method returns a ViewResult. View方法返回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. 唯一的区别是,使用ActionResult,您的控制器不承诺返回视图 - 您可以更改方法体以有条件地返回RedirectResult或其他内容,而无需更改方法定义。


#5楼

ActionResult is an abstract class that can have several subtypes. ActionResult是一个抽象类,可以有几个子类型。

ActionResult Subtypes ActionResult子类型

  • ViewResult - Renders a specifed view to the response stream ViewResult - 将指定的视图呈现给响应流

  • PartialViewResult - Renders a specifed partial view to the response stream PartialViewResult - 将指定的部分视图呈现给响应流

  • EmptyResult - An empty response is returned EmptyResult - 返回空响应

  • RedirectResult - Performs an HTTP redirection to a specifed URL RedirectResult - 执行HTTP重定向到指定的URL

  • RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data RedirectToRouteResult - 根据给定的路由数据执行HTTP重定向到由路由引擎确定的URL

  • JsonResult - Serializes a given ViewData object to JSON format JsonResult - 将给定的ViewData对象序列化为 JSON格式

  • JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client JavaScriptResult - 返回可在客户端上执行的一段JavaScript代码

  • ContentResult - Writes content to the response stream without requiring a view ContentResult - 无需视图即可将内容写入响应流

  • FileContentResult - Returns a file to the client FileContentResult - 将文件返回给客户端

  • FileStreamResult - Returns a file to the client, which is provided by a Stream FileStreamResult - 将文件返回给客户端,该文件由Stream提供

  • FilePathResult - Returns a file to the client FilePathResult - 将文件返回给客户端

Resources 资源


#6楼

In Controller i have specified the below code with ActionResult which is a base class that can have 11 subtypes in MVC like: ViewResult, PartialViewResult, EmptyResult, RedirectResult, RedirectToRouteResult, JsonResult, JavaScriptResult, ContentResult, FileContentResult, FileStreamResult, FilePathResult. 在Controller中,我使用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
    }

Likewise we can return all these 11 subtypes by using ActionResult() without specifying every subtype method explicitly. 同样,我们可以通过使用ActionResult()返回所有这11个子类型,而无需显式指定每个子类型方法。 ActionResult is the best thing if you are returning different types of views. 如果要返回不同类型的视图,ActionResult是最好的选择。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值