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

示例代码:   

    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.   }

最新文章

  1. 企业IT管理员IE11升级指南【16】—— 使用Compat Inspector快速定位IE兼容性问题
  2. 错误 1 error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. d:\users\vs2013\le
  3. windows10的激活及SQL Server 2008 的安装
  4. OC 内存泄露 自动释放池
  5. WordPress 博客文章时间格式the_time()设置
  6. IDEA 创建Java Web项目
  7. Gym 100531H Problem H. Hiking in the Hills 二分
  8. js 动态添加元素(div、li、img等)及设置属性
  9. extjs6整合到web项目中
  10. shell读取文件参数
  11. 获取对象属性类型、属性名称、属性值的研究:反射和JEXL解析引擎
  12. 从零搭建Vue开发环境
  13. cocos2dx 3.3 笔记
  14. NOIP 普及组 2014 螺旋矩阵
  15. MonkeyRunner_真机_运行脚本(二)
  16. python---django中模板渲染(csrf令牌使用,自定义模板函数)
  17. pytorch之LSTM
  18. jquery笔记一——小问题+小技巧
  19. 17个CSS知识点整理
  20. order by MetadataToken解决反射字段顺序问题

热门文章

  1. 重构if...else...或者switch程序块 为 中介者(Mediator)模式.的思考
  2. yiic创建YII应用 "php.exe"不是内部或外部命令 解决办法
  3. 【原创】开机出现grub rescue,修复办法
  4. asp.net上传Excel文件到服务端进行读取
  5. python zookeeper 学习笔记
  6. 基于C#的SolidWorks插件开发(2)--创建插件
  7. informix 查看数据库空间名
  8. ASP.NET Mvc Razor视图语法
  9. vi 在行首尾添加字符串
  10. UI表单