MVC3中Action返回类型ActionResult类型
MVC3中Action返回类型ActionResult在System.Web.Mvc命名空间中.这些包含在控制器中的方法,我们称为控制器中的 Action,比如:HomeController 中的 Index 方法就是一个 Action,这些 Action 的作用就是处理请求,然后返回对请求的处理结果。
ActionResult是一个抽象类, 在Action中返回的都是其派生类.下面是我整理的ASP.NET MVC 1.0 版本中提供的ActionResult派生类:
| 类名 | 抽象类 | 父类 | 功能 |
| ContentResult | 根据内容的类型和编码,数据内容. | ||
| EmptyResult | 空方法. | ||
| FileResult | abstract | 写入文件内容,具体的写入方式在派生类中. | |
| FileContentResult | FileResult | 通过 文件byte[] 写入文件. | |
| FilePathResult | FileResult | 通过 文件路径 写入文件. | |
| FileStreamResult | FileResult | 通过 文件Stream 写入文件. | |
| HttpUnauthorizedResult | 抛出401错误 | ||
| JavaScriptResult | 返回javascript文件 | ||
| JsonResult | 返回Json格式的数据 | ||
| RedirectResult | 使用Response.Redirect重定向页面 | ||
| RedirectToRouteResult | 根据Route规则重定向页面 | ||
| ViewResultBase | abstract | 调用IView.Render() | |
| PartialViewResult | ViewResultBase | 调用父类ViewResultBase 的ExecuteResult方法. 重写了父类的FindView方法. 寻找用户控件.ascx文件 |
|
| ViewResult | ViewResultBase | 调用父类ViewResultBase 的ExecuteResult方法. 重写了父类的FindView方法. 寻找页面.aspx文件 |
示例代码:
- public class ActionResultController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult ContentResult()
- {
- return Content("Hi, 我是ContentResult结果");
- }
- public ActionResult EmptyResult()
- {
- //空结果当然是空白了!
- //至于你信不信, 我反正信了
- return new EmptyResult();
- }
- public ActionResult FileResult()
- {
- var imgPath = Server.MapPath("~/demo.jpg");
- return File(imgPath, "application/x-jpg", "demo.jpg");
- }
- public ActionResult HttpNotFoundResult()
- {
- return HttpNotFound("Page Not Found");
- }
- public ActionResult HttpUnauthorizedResult()
- {
- //未验证时,跳转到Logon
- return new HttpUnauthorizedResult();
- }
- public ActionResult JavaScriptResult()
- {
- string js = "alert(\"Hi, I'm JavaScript.\");";
- return JavaScript(js);
- }
- public ActionResult JsonResult()
- {
- var jsonObj = new
- {
- Id = 1,
- Name = "小铭",
- Sex = "男",
- Like = "足球"
- };
- return Json(jsonObj, JsonRequestBehavior.AllowGet);
- }
- public ActionResult RedirectResult()
- {
- return Redirect("~/demo.jpg");
- }
- public ActionResult RedirectToRouteResult()
- {
- return RedirectToRoute(new {
- controller = "Hello", action = ""
- });
- }
- public ActionResult ViewResult()
- {
- return View();
- }
- public ActionResult PartialViewResult()
- {
- return PartialView();
- }
- //禁止直接访问的ChildAction
- [ChildActionOnly]
- public ActionResult ChildAction()
- {
- return PartialView();
- }
- //正确使用ChildAction
- public ActionResult UsingChildAction()
- {
- return View();
- }
- }
MVC3中Action返回类型ActionResult类型的更多相关文章
- Controller 中Action 返回值类型 及其 页面跳转的用法
•Controller 中Action 返回值类型 View – 返回 ViewResult,相当于返回一个View 页面. -------------------------------- ...
- Structs2中Action返回json到前台方法
1.传统方式JSON输出 这一点跟传统的Servlet的处理方式基本上一模一样,代码如下 01 public void doAction() throws IOException{ 02 ...
- ASP.NET Core MVC中的IActionFilter.OnActionExecuted方法执行时,Controller中Action返回的对象是否已经输出到Http Response中
我们在ASP.NET Core MVC项目中有如下HomeController: using Microsoft.AspNetCore.Mvc; namespace AspNetCoreActionF ...
- Controller中方法返回值其他类型需要添加jackson依赖
第一个 第二个: 第三个 https://www.cnblogs.com/codejackanapes/p/5569013.html:json的博客园 springmvc默认的是:2.Jackson: ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
- asp.net mvc 3.0 知识点整理 ----- (2).Controller中几种Action返回类型对比
通过学习,我们可以发现,在Controller中提供了很多不同的Action返回类型.那么具体他们是有什么作用呢?它们的用法和区别是什么呢?通过资料书上的介绍和网上资料的查询,这里就来给大家列举和大致 ...
- 【MVC】关于Action返回结果类型的事儿(上)
一. ASP.NET MVC 1.0 Result 几何? Action的返回值类型到底有几个?咱们来数数看. ASP.NET MVC 1.0 目前一共提供了以下十几种Action返回结果类型: 1. ...
- MVC Action 返回类型[转]
一. ASP.NET MVC 1.0 Result 几何? Action的返回值类型到底有几个?咱们来数数看. ASP.NET MVC 1.0 目前一共提供了以下十几种Action返回 ...
- ASP.NET MVC – 关于Action返回结果类型的事儿(上)
原文:ASP.NET MVC – 关于Action返回结果类型的事儿(上) 本文转自:博客园-文超的技术博客 一. ASP.NET MVC 1.0 Result 几何? Action的 ...
随机推荐
- QML按键事件处理
QML提供了对应的按键处理方法,我们接下来实现一个通过键盘上的方向键来移动文本,代码如下: import QtQuick 2.4 import QtQuick.Controls 1.3 import ...
- ios中XPath的语法
在XML的读写中,我们知道有Xpath的语法 1.定位节点:(/)代表绝对的路径,代表起始地位置.(//)表示文件中所有符合模式的元素都会被选出来,即使是处于树中不同的层级也会被选出来 2.KissX ...
- winform水晶报表编译错误,未能加载文件或程序集"file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framewor "
未能加载文件或程序集“file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Commo ...
- rsync与inotify 数据同步
发布:thebaby 来源:脚本学堂 [大 中 小] 本文介绍下,在linux系统中,使用rsync与inotify实现数据同步的一个实例,有研究文件同步的朋友可以作个参考.本文转自:ht ...
- IE6下input标签border问题
IE6下input标签的border的样式border:none;是不起作用的!要设置border:0px;才行!
- 压力测试之apache benchmark
ab 的全称是 ApacheBench , 是 Apache 附带的一个小工具 , 专门用于 HTTP Server 的 benchmark testing , 可以同时模拟多个并发请求.前段时间看到 ...
- 整理grep实战文本搜索过滤技巧
一:grep的简介: 文本搜索工具,根据用户指定的文本模式对目标文件进行逐行搜索,显示能够被模式所匹配到的行.配合正则表达式的使用可以实现强大的文本处理.下面一一说明正则的例子. 二:文本处理工具分类 ...
- Python开发轻量级爬虫
这两天自学了python写爬虫,总结一下: 开发目的:抓取百度百科python词条页面的1000个网页 设计思路: 1,了解简单的爬虫架构: 2,动态的执行流程: 3,各部分的实现: URL管理器:p ...
- 【hibernate】spring+ jpa + hibername 配置过程遇到的问题
org.springframework.data.jpa.domain.support.AuditingEntityListener Auditing: 审计:查账Hades:Hades是一个开源库, ...
- how to run demo city bars using sencha architect
1. create a project using city bars template in sencha architect 2. save your project name as CityBa ...