ASP.NET MVC ActionResult的实现
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的实现的更多相关文章
- asp.net mvc ActionResult
定义在Controller中的Action方法大都返回一个ActionResult对象.ActionResult是对Action执行结果的封装,用于最终对请求进行响应.ASP.NET MVC提供了一系 ...
- ASP.NET MVC ActionResult的其它返回值
一.ascx页面 场景:要返回代码片断,比如Ajax返回一个子页 我们先新建一个Action public ActionResult Ascx() { return PartialView(); } ...
- 列举mvc ActionResult的返回值
8.列举ASP.NET MVC ActionResult的返回值有几种类型? 主要有View(视图).PartialView(部分视图).Content(内容).Json(Json字符串).Javas ...
- 【转】ASP.NET MVC学习笔记-Controller的ActionResult
1. 返回ViewResult public ActionResult Index() { ViewData["Message"] = "Welcome ...
- 了解ASP.NET MVC几种ActionResult的本质:JavaScriptResult & JsonResult
在之前的两篇文章(<EmptyResult & ContentResult>和<FileResult>)我们剖析了EmptyResult.ContentResult和F ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
- 了解ASP.NET MVC几种ActionResult的本质:HttpStatusCodeResult & RedirectResult/RedirectToRouteResult
在本系列的最后一篇,我们来讨论最后三个ActionResult:HttpStatusCodeResult.RedirectResult和RedirectToRouteResult .第一个用于实现针对 ...
- [转载]深入理解ASP.NET MVC之ActionResult
Action全局观 在上一篇最后,我们进行到了Action调用的“门口”: 1 if (!ActionInvoker.InvokeAction(ControllerContext, actionNam ...
- Asp.net MVC 之 ActionResult
Action运行完后,回传的值通过ActionResult 类别或者其衍生的类别操作.ActionResult是一个抽象类,因此,Asp.net MVC 本身就实作了许多不同类型的ActionResu ...
随机推荐
- 设置配置文件信息时的classpath
首先 classpath是指 WEB-INF文件夹下的classes目录 其中:lib和classes下文件访问优先级的问题: lib>classes classpath 和 classpa ...
- 2015年最新中国知网CNKI免费账号直接入口
以下是Free9免费资源网小编收集整理的2015年最新中国知网CNKI免费账号直接入口,现免费分享给大家(仅供测试使用),此类文献数据库资源有时效性,希望对您的学习.工作上有所帮助! 中国知网直接入口 ...
- Feature Flag
know more from here: https://www.youtube.com/watch?v=WMRjj06R6jg&list=UUkQX1tChV7Z7l1LFF4L9j_g F ...
- POJ 1733 Parity game(离散化+带权并查集)
离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...
- POJ1008Maya Calendar
http://poj.org/problem?id=1008&lang=default&change=true 这个题倒是不难,就是麻烦一点,但是还WA了几次都是因为处理天数的时候没处 ...
- Oracle 体系结构2 - 实例和数据库
Oracle最最基本的概念: 实例和数据库 实例就是oracle进程和一块共享内存, 数据库就是静态的文件,如datafile, log file, redo logfile, control fil ...
- kindeditor.net应用
1.网址:http://kindeditor.net/docs/usage.html
- 为什么需要用到序列化?为什么HttpSession中对象要序列化
简单说就是为了保存在内存中的各种对象的状态,并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存Object States,但是Java给你提供一种应该比你自己好的保存对象状态的 ...
- spring3.0的jar包详解
1. spring.jar 是包含有完整发布模块的单个jar 包. 2. org.springframework.aop 包含在应用中使用Spring的AOP特性时所需的类. 3. org.sprin ...
- React使用rAF动画介绍
一. <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF ...