Controller 类中返回各种类型的 ActionResult 的 Action 实例
当前我也在努力学习MVC中,觉得子秋的这个系列搞得挺不错的,所以把部分转过来保存,便于以后忘记了查找(好记性不如烂笔头嘛),呵呵。
Code
1 public class DemoController : Controller
2 {
3 /// <summary>
4 /// http://localhost:1847/Demo/ContentResultDemo
5 /// </summary>
6 /// <returns></returns>
7 public ActionResult ContentResultDemo()
8 {
9 string contentString = "ContextResultDemo!";
10 return Content(contentString);
11 }
12
13 /// <summary>
14 /// http://localhost:1847/Demo/EmptyResultDemo
15 /// </summary>
16 /// <returns></returns>
17 public ActionResult EmptyResultDemo()
18 {
19 return new EmptyResult();
20 }
21
22 /// <summary>
23 /// http://localhost:1847/Demo/FileContentResultDemo
24 /// </summary>
25 /// <returns></returns>
26 public ActionResult FileContentResultDemo()
27 {
28 FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
29 byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
30 fs.Read(buffer, 0, Convert.ToInt32(fs.Length) );
31 return File(buffer, @"image/gif");
32 }
33
34 /// <summary>
35 /// http://localhost:1847/Demo/FilePathResultDemo
36 /// </summary>
37 /// <returns></returns>
38 public ActionResult FilePathResultDemo()
39 {
40 //可以将一个jpg格式的图像输出为gif格式
41 return File(Server.MapPath(@"/resource/Images/2.jpg"), @"image/gif");
42 }
43
44 /// <summary>
45 /// http://localhost:1847/Demo/FileStreamResultDemo
46 /// </summary>
47 /// <returns></returns>
48 public ActionResult FileStreamResultDemo()
49 {
50 FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
51 return File(fs, @"image/gif");
52 }
53
54 /// <summary>
55 /// http://localhost:1847/Demo/HttpUnauthorizedResultDemo
56 /// </summary>
57 /// <returns></returns>
58 public ActionResult HttpUnauthorizedResultDemo()
59 {
60 return new HttpUnauthorizedResult();
61 }
62
63 /// <summary>
64 /// http://localhost:1847/Demo/JavaScriptResultDemo
65 /// </summary>
66 /// <returns></returns>
67 public ActionResult JavaScriptResultDemo()
68 {
69 return JavaScript(@"alert(""Test JavaScriptResultDemo!"")");
70 }
71
72 /// <summary>
73 /// http://localhost:1847/Demo/JsonResultDemo
74 /// </summary>
75 /// <returns></returns>
76 public ActionResult JsonResultDemo()
77 {
78 var tempObj = new { Controller = "DemoController", Action = "JsonResultDemo" };
79 return Json(tempObj);
80 }
81
82 /// <summary>
83 /// http://localhost:1847/Demo/RedirectResultDemo
84 /// </summary>
85 /// <returns></returns>
86 public ActionResult RedirectResultDemo()
87 {
88 return Redirect(@"http://localhost:1847/Demo/ContentResultDemo");
89 }
90
91 /// <summary>
92 /// http://localhost:1847/Demo/RedirectToRouteResultDemo
93 /// </summary>
94 /// <returns></returns>
95 public ActionResult RedirectToRouteResultDemo()
96 {
97 return RedirectToAction(@"FileStreamResultDemo");
98 }
99
100 /// <summary>
101 /// http://localhost:1847/Demo/PartialViewResultDemo
102 /// </summary>
103 /// <returns></returns>
104 public ActionResult PartialViewResultDemo()
105 {
106 return PartialView();
107 }
108
109 /// <summary>
110 /// http://localhost:1847/Demo/RedirectToRouteResultDemo
111 /// </summary>
112 /// <returns></returns>
113 public ActionResult ViewResultDemo()
114 {
115 //如果没有传入View名称, 默认寻找与Action名称相同的View页 面.
116 return View();
117 }
118
119 }
1 public class DemoController : Controller
2 {
3 /// <summary>
4 /// http://localhost:1847/Demo/ContentResultDemo
5 /// </summary>
6 /// <returns></returns>
7 public ActionResult ContentResultDemo()
8 {
9 string contentString = "ContextResultDemo!";
10 return Content(contentString);
11 }
12
13 /// <summary>
14 /// http://localhost:1847/Demo/EmptyResultDemo
15 /// </summary>
16 /// <returns></returns>
17 public ActionResult EmptyResultDemo()
18 {
19 return new EmptyResult();
20 }
21
22 /// <summary>
23 /// http://localhost:1847/Demo/FileContentResultDemo
24 /// </summary>
25 /// <returns></returns>
26 public ActionResult FileContentResultDemo()
27 {
28 FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
29 byte[] buffer = new byte[Convert.ToInt32(fs.Length)];
30 fs.Read(buffer, 0, Convert.ToInt32(fs.Length) );
31 return File(buffer, @"image/gif");
32 }
33
34 /// <summary>
35 /// http://localhost:1847/Demo/FilePathResultDemo
36 /// </summary>
37 /// <returns></returns>
38 public ActionResult FilePathResultDemo()
39 {
40 //可以将一个jpg格式的图像输出为gif格式
41 return File(Server.MapPath(@"/resource/Images/2.jpg"), @"image/gif");
42 }
43
44 /// <summary>
45 /// http://localhost:1847/Demo/FileStreamResultDemo
46 /// </summary>
47 /// <returns></returns>
48 public ActionResult FileStreamResultDemo()
49 {
50 FileStream fs = new FileStream(Server.MapPath(@"/resource/Images/1.gif"), FileMode.Open, FileAccess.Read);
51 return File(fs, @"image/gif");
52 }
53
54 /// <summary>
55 /// http://localhost:1847/Demo/HttpUnauthorizedResultDemo
56 /// </summary>
57 /// <returns></returns>
58 public ActionResult HttpUnauthorizedResultDemo()
59 {
60 return new HttpUnauthorizedResult();
61 }
62
63 /// <summary>
64 /// http://localhost:1847/Demo/JavaScriptResultDemo
65 /// </summary>
66 /// <returns></returns>
67 public ActionResult JavaScriptResultDemo()
68 {
69 return JavaScript(@"alert(""Test JavaScriptResultDemo!"")");
70 }
71
72 /// <summary>
73 /// http://localhost:1847/Demo/JsonResultDemo
74 /// </summary>
75 /// <returns></returns>
76 public ActionResult JsonResultDemo()
77 {
78 var tempObj = new { Controller = "DemoController", Action = "JsonResultDemo" };
79 return Json(tempObj);
80 }
81
82 /// <summary>
83 /// http://localhost:1847/Demo/RedirectResultDemo
84 /// </summary>
85 /// <returns></returns>
86 public ActionResult RedirectResultDemo()
87 {
88 return Redirect(@"http://localhost:1847/Demo/ContentResultDemo");
89 }
90
91 /// <summary>
92 /// http://localhost:1847/Demo/RedirectToRouteResultDemo
93 /// </summary>
94 /// <returns></returns>
95 public ActionResult RedirectToRouteResultDemo()
96 {
97 return RedirectToAction(@"FileStreamResultDemo");
98 }
99
100 /// <summary>
101 /// http://localhost:1847/Demo/PartialViewResultDemo
102 /// </summary>
103 /// <returns></returns>
104 public ActionResult PartialViewResultDemo()
105 {
106 return PartialView();
107 }
108
109 /// <summary>
110 /// http://localhost:1847/Demo/RedirectToRouteResultDemo
111 /// </summary>
112 /// <returns></returns>
113 public ActionResult ViewResultDemo()
114 {
115 //如果没有传入View名称, 默认寻找与Action名称相同的View页 面.
116 return View();
117 }
118
119 }