理解ASP.NET MVC中的ActionResult [转 深山老林]
http://www.cnblogs.com/wlb/archive/2009/12/10/1620790.html
通常我们在一个ASP.NET MVC项目中创建一个Controller的时候,Index()方法默认的返回类型都是ActionResult,通过查看UML图,ActionResult实际上是一个抽象类,因此实际返回的类型是该抽象类的子类。
Ø ActionResult及其子类的对照表
ActionResult的子类名称 |
说明 |
ViewResult |
表示HTML的页面内容 |
EmptyResult |
表示空白的页面内容 |
RedirectResult |
表示定位到另外一个URL |
JsonResult |
表示可以运用到AJAX程序中JSON结果 |
JavaScriptResult |
表示一个JavaScript对象 |
ContentResult |
表示一个文本内容 |
FileContentResult |
表示一个可以下载的、二进制内容的文件 |
FilePathResult |
表示一个可以下载的、指定路径的文件 |
FileStreamResult |
表示一个可以下载的、流式的文件 |
通过我们在代码中编写,不难看到如下的一段代码:
public ActionResult Index()
{
return View();
}
可能有人会有疑问,既然我定义的是ActionResult,为什么返回值会是View呢?
其实这个View的类型是ActionResult的子类ViewResult,有关Controller中的方法与返回对象请参照下表:
Controller中的方法 |
返回对象 |
View |
ViewResult |
Redirect |
RedirectResult |
RedirectToAction |
RedirectToActionResult |
RedirectToRoute |
RedirectToRouteResult |
Json |
JsonResult |
JavaScriptResult |
JavaScriptResult |
Content |
ContentResult |
File |
FileContentResult、FilePathResult、FileStreamResult |
例子:
public ActionResult query_cookie_info(){bool loginStatus = false;string _nk_ = string.Empty;if (isAuthenticated()){loginStatus = true;SystemUser systemUser = this.iUserService.GetOrCreateUser(this.HttpContext);_nk_ = getGracefulName(systemUser);}string script = "var userCookie={_nk_:'" + _nk_ + "',_l_g_:'" + (loginStatus?"1":"") +"',uc1:'',mt:'',l:'',version:''};TB && TB.Global && TB.Global.run && TB.Global.run();";return JavaScript(script);}
例子:
public ActionResult login(LoginModel loginModel,string tpl_redirect_url,string style){bool remember=false;string userID = string.Empty;if (vr.isSucceed){ userID = loginUser.SystemUser.Id;populateFormAuthCookie(remember, userID, "");if (false /*is full login*/){return RedirectToAction("MyCart", new { controller = "Home", area = "Cart" });}return Redirect(tpl_redirect_url);} 。。。。。
例子:
[Description("显示相应的类别对应的新闻")]public ActionResult GetNewsBySectionID(string sectionID, int? pageIndex, int? pageSize, string viewName){int _pageSize = pageSize ?? 10;int _pageIndex = pageIndex ?? 0;string _viewName = viewName ?? "LiWithEndDateWidget";IList<DataResource> listnews = _newsRepository.GetNewsBySectionID(sectionID, _pageIndex, _pageSize);return View(_viewName, listnews);}
例子:
[Description("点击dtree时根据点击的节点转到不同的页面")]public ActionResult TreeResearchView(string flag){string MetaSetID = flag;return RedirectToAction("TreeResearch", "MetaObjectService", new { MSID = MetaSetID });}
RedirectToAction方式从一个action跳转到另一个action
重定向到UserController.cs中的Edit()函数,如果不在同一个Controller中,那么雷同重定向到HomeController.cs中的Index()函数
转载于:https://www.cnblogs.com/wukong0214/archive/2013/03/11/2953467.html
理解ASP.NET MVC中的ActionResult [转 深山老林]相关推荐
- ASP.NET MVC中常用的ActionResult类型
一.定义 MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...
- 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC
七天学会ASP.NET MVC (一)--深入理解ASP.NET MVC 系列文章 七天学会ASP.NET MVC (一)--深入理解ASP.NET MVC 七天学会ASP.NET MVC (二)-- ...
- 在ASP.NET MVC中使用IIS级别的URL Rewrite
在ASP.NET MVC中使用IIS级别的URL Rewrite 原文 在ASP.NET MVC中使用IIS级别的URL Rewrite 大约一年半前,我在博客上写过一系列关于URL Rewrite的 ...
- 在Asp.Net MVC中使用ModelBinding构造Array、List、Collection以及Dictionary
在asp.net mvc中,我们可以在html表单中使用特定的格式传递参数,从而通过model binder构造一些集合类型. 第一种方式 public ActionResult Infancy(Pe ...
- 通过源代码研究ASP.NET MVC中的Controller和View(二)
通过源代码研究ASP.NET MVC中的Controller和View(一) 在开始之前,先来温习下上一篇文章中的结论(推论): IView是所有HTML视图的抽象 ActionResult是Cont ...
- ASP.NET MVC中你必须知道的13个扩展点
ScottGu在其最新的博文中推荐了Simone Chiaretta的文章13 ASP.NET MVC extensibility points you have to know,该文章为我 ...
- MVC中的ActionResult
本文导读:MVC中ActionResult是Action的返回结果.ActionResult 有多个派生类,每个子类功能均不同,并不是所有的子类都需要返回视图View,有些直接返回流,有些返回字符串等 ...
- ASP.NET MVC中实现多个按钮提交的几种方法
有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...
- 在asp.net mvc中使用PartialView返回部分HTML段
问题链接: MVC怎样实现异步调用输出HTML页面 该问题是个常见的 case, 故写篇文章用于提示新人. 在asp.net mvc中返回View时使用的是ViewResult,它继承自ViewRes ...
最新文章
- SpringBoot 【IDEA热部署+浏览器禁用缓存】迅速提升效率
- 图解Hadoop hdfs的基本工作机制及相关概念
- 转——C# DataGridView控件 动态添加新行
- 使用ACE_SOCK_SEQPACK_Association获取socket连接的本地及远程IP端口信息
- 基于Curator实现dubbo服务自动注册发现
- cool 软件 —— Carnac(实时桌面显示按键)
- 解密 TCP/IP!
- [LeetCode]Rotate List
- 苹果手机投屏电脑win7
- 校园学生管理系统设想
- isbn书号查询php代码,php根据isbn书号查询amazon网站上的图书信息的示例
- 无老师作文巨讲堂2015.8
- 《猎头局中局》——摘要
- Oracle EBS 笔记8
- android卡刷教程,卡刷是什么意思?安卓系统卡刷教程详解
- JDK参考文档的使用
- python一种基于电脑mac地址的加密算法
- 冬色烂漫 纯情踏雪 论著名画家冯庆冰雪画作品
- 新学期|新气象,青春有为,不负韶华
- stm32f105vct6例程_STM32F105VCT6_USB_TEST
热门文章
- Revive: Rebalancing Off-lockchain Payment Networks 阅读笔记
- 蓝桥杯中的暴力破解 ing
- 好看又能打的CRM系统大比拼:Salesforce, SugarCRM, Odoo等
- iOS-网络流量统计
- fortran使用MKL函数库计算一个复数向量的共轭与另一个复数向量的内积
- 训练神经网络的技巧总结
- 北京市中关村等7地区未来三年将免费无线上网
- Eclipse笔记-Build path entry is missing: org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.inter
- Android设计相关的网站推荐
- lucenesolr大盘点