1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace MVC知识点.Controllers
8 {
9 using System.Drawing;
10 public class C02ActionResultSubClassController : Controller
11 {
12
13 #region 1.0 ViewResult 用于返回一个试图页面
14
15 public ActionResult Index()
16 {
17 return View();
18 }
19 #endregion
20
21 #region 2.0 ContentResult子类用于返回一个文本字符串
22
23 public ActionResult ContentResultDemo()
24 {
25 return Content("<h2>你好,天安门~~~</h2> <script>alert('ok,我被执行了!')</script>");
26 }
27
28 #endregion
29
30 #region 3.0 EmptyResult 用于 占位
31
32 public ActionResult EmptyResultDemo()
33 {
34 return new EmptyResult();
35 }
36
37 #endregion
38
39 #region 4.0 JsonResult 配合ajax请求使用的 (重点)
40
41 /// <summary>
42 /// 如果是GET请求此方法则会报错:
43 /// 此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。
44 /// 在Json()方法中加上, JsonRequestBehavior.AllowGet 即可解决
45 /// 如果是POST可加可不加
46 /// </summary>
47 /// <returns></returns>
48 public ActionResult JsonResult()
49 {
50 //自动将传入的对象序列化成json格式写入到response对象中
51 return Json(new { Name = " 八戒", Age = 500 }, JsonRequestBehavior.AllowGet);
52 }
53
54 #endregion
55
56 #region 5.0 FileResult 实现验证码的产生(重点)
57
58 public ActionResult FileReulstDemo()
59 {
60 //1.0
61 string vcode = "123";
62
63 //2.0 将验证码写入session
64 Session["vcode"] = vcode;
65
66 //3.0 画验证码
67 //3.0.1 定义一个图片对象,注意using system.drawing
68 byte[] imgbuffer;
69 using (Image img = new Bitmap(60, 25))
70 {
71 //3.0.2 定义一个画家对象,注意画家对象来源于3.0.1定义的图片对象
72 using (Graphics g = Graphics.FromImage(img))
73 {
74 //3.0.3 利用画家对象画字符串 DrawString()
75 g.Clear(Color.White);
76 g.DrawRectangle(Pens.Blue, new Rectangle(0, 0, img.Width - 1, img.Height - 1));
77 g.DrawString(vcode, new Font("黑体", 16, FontStyle.Bold | FontStyle.Italic | FontStyle.Strikeout), new SolidBrush(Color.Red), 5, 5);
78 }
79
80 //3.0.4 将图片对象转换成byte[]数组
81 using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
82 {
83 img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
84 //3.0.5 将ms内存流写入imgbuffer
85 imgbuffer = ms.ToArray();
86 }
87 }
88
89 //3.0.4 将图片对象写入到响应流中
90 return File(imgbuffer, "image/jpeg");
91 }
92
93 #endregion
94
95 #region 6.0 HttpStatusCodeResult 实现修改返回的状态码(盗链请求,SEO,网络爬虫)
96
97 public ActionResult HttpStatusCodeResultDemo()
98 {
99 return new HttpStatusCodeResult(500);
100 return new HttpStatusCodeResult(404);
101 }
102
103 #endregion
104
105 #region 7.0 JavaScriptResult 演示 (专门是给ajax的请求来使用的)
106
107 public ActionResult CallJs()
108 {
109 return View();
110 }
111
112 public ActionResult JavaScriptResultDemo()
113 {
114 return JavaScript("alert('ok,被执行了')");
115 }
116
117 #endregion
118
119 #region 8.0 RedirectResult 跳转到指定的url (重点)
120
121 public ActionResult RedirectResultDemo()
122 {
123 //跳转到任意一个控制器中的方法
124 return Redirect("/C02ActionResultSubClass/FileReulstDemo");
125 }
126
127 public ActionResult RedirectResultAction()
128 {
129 //跳转到同一个控制器的另外一个方法
130 return RedirectToAction("FileReulstDemo");
131 }
132
133 public ActionResult RedirectResultRoutName()
134 {
135 //当网站有多条路由规则的时候,可以指定具体的路由规则来生成跳转url,再进行跳转
136 return RedirectToRoute("Default", new { controller = "C02ActionResultSubClass", action = "ContentResultDemo" });
137 }
138
139 #endregion
140
141 }
142 }