本文翻译自: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 . 其他派生类包括JsonResult和PartialViewResult

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 ViewResultelsereturn 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 资源

  • What's the difference between ActionResult and ViewResult for action method? ActionResult和ViewResult for action方法有什么区别? [ASP.NET Forums] [ASP.NET论坛]

#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是最好的选择。

ViewResult()和ActionResult()之间的区别相关推荐

  1. FPGA与ASIC:它们之间的区别以及使用哪一种?

    FPGA与ASIC:它们之间的区别以及使用哪一种? FPGA Vs ASIC: Differences Between Them And Which One To Use? VL82C486 Sing ...

  2. Python 应用领域以及版本之间的区别

    Python 应用领域以及版本之间的区别 一.Python应用领域 Python+人工智能,给你更多研究方向选择! 企业级综合实战项目,集六大前沿技术为一体 二. Python 2与Python 3的 ...

  3. java中separator_java - File.separator和路径中的斜杠之间的区别

    java - File.separator和路径中的斜杠之间的区别 在Java Path-String中使用/和普通的File.separator有什么区别? 与双反斜杠相比,/平台独立似乎不是原因, ...

  4. 机器学习、数据科学、人工智能、深度学习和统计学之间的区别!

    ↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale干货 作者:Vincent Granville,来源:机器之心 在这篇文章中, ...

  5. Python里面None True False之间的区别

    None虽然跟True False一样都是布尔值. 虽然None不表示任何数据,但却具有很重要的作用. 它和False之间的区别还是很大的! 例子: >>> t = None > ...

  6. 2.javascript之缓存 localStorage 和sessionStorage之间的区别

    2018-08-04 前言 今天做项目的时候遇到了这个问题,用户登录成功之后如何改变将登录的链接切换为用户名 解决方案:使用了sessionstorage缓存 和js的onload加载事件 用户登录成 ...

  7. java se 与j2se_关于java:J2EE和J2SE项目之间的区别

    本问题已经有最佳答案,请猛点这里访问. 我已经从github下载了一个开源项目. 这是一个基于Maven的项目. 我如何理解该项目是J2SE项目还是J2EE项目? 这两种项目在结构上有何不同? Jav ...

  8. oracle类型sql转为mysql_Oracle和MySql之间SQL区别(等效转换以及需要注意的问题)...

    >本篇博文是Oracle和MySQL之间的等效SQL转换和不同,目前市面上没有转换两种SQL的工具,小编觉得以后也不一定会有,于是在业余时间整理了一下,如果有什么错误之处请留言告知,小编也是刚 ...

  9. 腾讯面试题:char 和 varchar的最大长度是多少,以及他们之间的区别(看完你就能和面试官笑谈人生了)

    title: 腾讯面试题:char 和 varchar的最大长度是多少,以及他们之间的区别(看完你就能和面试官笑谈人生了) tags: 面试常见题 腾讯面试题:char 和 varchar的最大长度是 ...

最新文章

  1. apache .htaccess 禁止访问某目录方法
  2. unicode环境下用CFile读取txt的若干疑惑,该如何处理
  3. r740服务器增加内存,戴尔R740服务器获取cpu、内存、硬盘参数信息。
  4. QT的QDesignerWidgetBoxInterface类的使用
  5. 《计算机网络》读书笔记(一)--计算机网络体系结构
  6. 抓包软件:Charles
  7. Windows函数错误处理
  8. Android中文API(128) —— HandlerThread
  9. byte 转 int 为什么要0xFF?
  10. 嵌入式论文3000字_普通期刊发表论文费用是多少
  11. 期望dp--BZOJ3450 Easy
  12. 在CSDN的第0篇博客
  13. 技术干货:Linux Shell 编程基础,看这一篇就够了!
  14. android 浏览器隐藏地址,移动端隐藏手机浏览器的地址栏一下底部的菜单栏
  15. NB-IoT低功耗技术与寻呼
  16. 【子集/组合/全排列】C语言框架
  17. 密钥协商算法的演变 —— RSA算法 - DH算法 - DHE算法 - ECDHE算法
  18. uniapp日历插件
  19. 友盟php接入统计,ionic2 接入友盟统计
  20. python开发qq聊天机器人_Python qqbot 实现qq机器人的示例代码

热门文章

  1. 在看一个经典教材写的crontab时遇到的点小问题
  2. 企业注册一站式服务平台公司宝App挂牌新三板
  3. Android Studio(五):修改Android Studio项目包名
  4. Windows Server 2003 AD域升级至Windows Server 2008 R2实战案例
  5. 百度SMS发送短信C#
  6. 三、系统分层和分割策略
  7. YII2 rules 规则验证器
  8. 字符串的碎片整理。。。
  9. 为什么要使用boost::enable_shared_from_thisT
  10. 自己动手实现STL 02:构造析构的基本工具construct()和destroy()(stl_construct.h)