1、11种ActionResult

在System.Web.Mvc命名空间下:

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文件

2、代码

 1 public class ActionResultController : Controller
2 {
3 public ActionResult Index()
4 {
5 return View();
6 }
7 public ActionResult ContentResult()
8 {
9 return Content("Hi, 我是ContentResult结果");
10 }
11 public ActionResult EmptyResult()
12 {
13 //空结果当然是空白了!
14 //至于你信不信, 我反正信了
15 return new EmptyResult();
16 }
17 public ActionResult FileResult()
18 {
19 var imgPath = Server.MapPath("~/demo.jpg");
20 return File(imgPath, "application/x-jpg", "demo.jpg");
21 }
22 public ActionResult HttpNotFoundResult()
23 {
24 return HttpNotFound("Page Not Found");
25 }
26 public ActionResult HttpUnauthorizedResult()
27 {
28 //未验证时,跳转到Logon
29 return new HttpUnauthorizedResult();
30 }
31 public ActionResult JavaScriptResult()
32 {
33 string js = "alert(\"Hi, I'm JavaScript.\");";
34 return JavaScript(js);
35 }
36 public ActionResult JsonResult()
37 {
38 var jsonObj = new
39 {
40 Id = 1,
41 Name = "小铭",
42 Sex = "男",
43 Like = "足球"
44 };
45 return Json(jsonObj, JsonRequestBehavior.AllowGet);
46 }
47 public ActionResult RedirectResult()
48 {
49 return Redirect("~/demo.jpg");
50 }
51 public ActionResult RedirectToRouteResult()
52 {
53 return RedirectToRoute(new {
54 controller = "Hello", action = ""
55 });
56 }
57 public ActionResult ViewResult()
58 {
59 return View();
60 }
61 public ActionResult PartialViewResult()
62 {
63 return PartialView();
64 }
65 //禁止直接访问的ChildAction
66 [ChildActionOnly]
67 public ActionResult ChildAction()
68 {
69 return PartialView();
70 }
71 //正确使用ChildAction
72 public ActionResult UsingChildAction()
73 {
74 return View();
75 }
76 }

ASP.NET MVC ActionResult的实现的更多相关文章

  1. asp.net mvc ActionResult

    定义在Controller中的Action方法大都返回一个ActionResult对象.ActionResult是对Action执行结果的封装,用于最终对请求进行响应.ASP.NET MVC提供了一系 ...

  2. ASP.NET MVC ActionResult的其它返回值

    一.ascx页面 场景:要返回代码片断,比如Ajax返回一个子页 我们先新建一个Action public ActionResult Ascx() { return PartialView(); } ...

  3. 列举mvc ActionResult的返回值

    8.列举ASP.NET MVC ActionResult的返回值有几种类型? 主要有View(视图).PartialView(部分视图).Content(内容).Json(Json字符串).Javas ...

  4. 【转】ASP.NET MVC学习笔记-Controller的ActionResult

    1. 返回ViewResult public ActionResult Index()   {       ViewData["Message"] = "Welcome ...

  5. 了解ASP.NET MVC几种ActionResult的本质:JavaScriptResult & JsonResult

    在之前的两篇文章(<EmptyResult & ContentResult>和<FileResult>)我们剖析了EmptyResult.ContentResult和F ...

  6. Asp.net MVC 中Controller返回值类型ActionResult

    [Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...

  7. 了解ASP.NET MVC几种ActionResult的本质:HttpStatusCodeResult & RedirectResult/RedirectToRouteResult

    在本系列的最后一篇,我们来讨论最后三个ActionResult:HttpStatusCodeResult.RedirectResult和RedirectToRouteResult .第一个用于实现针对 ...

  8. [转载]深入理解ASP.NET MVC之ActionResult

    Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...

  9. Asp.net MVC 之 ActionResult

    Action运行完后,回传的值通过ActionResult 类别或者其衍生的类别操作.ActionResult是一个抽象类,因此,Asp.net MVC 本身就实作了许多不同类型的ActionResu ...

随机推荐

  1. 剑指offer--面试题19

    题目:求二叉树镜像 根据作者思路,自己所写代码如下: void BinaryTreeMirror(BinaryTreeNode* pRoot) { if(pRoot == NULL) return; ...

  2. cg 到hlsl的转换

    http://msdn.microsoft.com/en-us/library/windows/desktop/ff471376(v=vs.85).aspx http://gamedev.stacke ...

  3. 原 Linux搭建SVN 服务器

    原 Linux搭建SVN 服务器 发表于1年前(2014-08-05 17:55)   阅读(12257) | 评论(3) 31人收藏此文章, 我要收藏 赞3 摘要 Linux搭建SVN 服务器 目录 ...

  4. kill 非法用户

    主要涉及到的相关命令如:who/w/ps/kill/pkill/killall查看当前登录用户:[root@localhost ~]# whoroot     pts/1        2010-08 ...

  5. PHP7 扩展之自动化测试

    在安装 PHP7 及各种扩展的过程中,如果你是用源码安装,会注意到在 make 成功之后总会有一句提示:Don't forget to run 'make test'. 这个 make test 就是 ...

  6. 使用tornado让你的请求异步非阻塞

    http://www.dongwm.com/archives/shi-yong-tornadorang-ni-de-qing-qiu-yi-bu-fei-zu-sai/?utm_source=tuic ...

  7. Android开发--Activity生命周期回顾理解

    Activity和Servlet一样,都用了回调机制.我们通过类比servlet来学习Activity.当一个servlet开发出来之后,该servlet运行于Web服务器中.服务器何时创建servl ...

  8. 将集成spring的项目从tomcat上移植到weblogic下存在的问题

    当在weblogic下部署时, 1.需要jersey-servlet-xx.jar,jersey-core-xx.jar,jersey-server-xx.jar: 2.在web.xml中全局参数co ...

  9. SaaS系列介绍之十一: SaaS商业模式分析

    1 配置模式 中国企业很多是人治,管理弹性非常大,公司的政策经常变化,管理流程.业务变化也非常大,发展也非常快;一个公司今年是10个人,明年是100个人,后年可能是1000人.管理机制.方法处于经常变 ...

  10. hdu 1005 java(System.out.println();与System.out.println(“\n”);)

    //package Main; import java.util.Scanner; public class Main { static int [][] mat=new int [2][2]; st ...