【MVC】关于Action返回结果类型的事儿(下)

代码
  1  using System;
  2  using System.Collections.Generic;
  3  using System.Linq;
  4  using System.Web;
  5  using System.Web.Mvc;
  6  using System.Web.Mvc.Ajax;
  7 
  8  using System.IO;
  9 
10  namespace MVC.Controllers
11  {
12      ///   <summary>
13      /// Controller 类必须以字符串 "Controller" 做类名称的结尾,字符串 Controller 之前的字符串为 Controller 的名称,类中的方法名为 Action 的名称
14      ///   </summary>
15      public   class ControllerDemoController : Controller
16      {
17          // [NonAction] - 当前方法仅为普通方法,不解析为 Action
18          // [AcceptVerbs(HttpVerbs.Post)] - 声明 Action 所对应的 http 方法
19 
20          ///   <summary>
21          /// Action 可以没有返回值
22          ///   </summary>
23          public   void Void()
24          {
25              Response.Write( string .Format( " <span style='color: red'>{0}</span> " , " void " ));
26          }
27 
28          ///   <summary>
29          /// 如果 Action 要有返回值的话,其类型必须是 ActionResult
30          /// EmptyResult - 空结果
31          ///   </summary>
32          public ActionResult EmptyResult()
33          {
34              Response.Write( string .Format( " <span style='color: red'>{0}</span> " , " EmptyResult " ));
35              return   new EmptyResult();
36          }
37 
38          ///   <summary>
39          /// Controller.Redirect() - 转向一个指定的 url 地址
40          /// 返回类型为 RedirectResult
41          ///   </summary>
42          public ActionResult RedirectResult()
43          {
44              return   base .Redirect( " ~/ControllerDemo/ContentResult " );
45          }
46 
47          ///   <summary>
48          /// Controller.RedirectToAction() - 转向到指定的 Action
49          /// 返回类型为 RedirectToRouteResult
50          ///   </summary>
51          public ActionResult RedirectToRouteResult()
52          {
53              return   base .RedirectToAction( " ContentResult " );
54          }
55 
56          ///   <summary>
57          /// Controller.Json() - 将指定的对象以 JSON 格式输出出来
58          /// 返回类型为 JsonResult
59          ///   </summary>
60          public ActionResult JsonResult( string name)
61          {
62              System.Threading.Thread.Sleep( 1000 );
63 
64              var jsonObj =   new { Name = name, Age =   new Random().Next( 20 , 31 ) };
65              return   base .Json(jsonObj);
66          }
67 
68          ///   <summary>
69          /// Controller.JavaScript() - 输出一段指定的 JavaScript 脚本
70          /// 返回类型为 JavaScriptResult
71          ///   </summary>
72          public ActionResult JavaScriptResult()
73          {
74              return   base .JavaScript( " alert('JavaScriptResult') " );
75          }
76 
77          ///   <summary>
78          /// Controller.Content() - 输出一段指定的内容
79          /// 返回类型为 ContentResult
80          ///   </summary>
81          public ActionResult ContentResult()
82          {
83              string contentString =   string .Format( " <span style='color: red'>{0}</span> " , " ContentResult " );
84              return   base .Content(contentString);
85          }
86 
87          ///   <summary>
88          /// Controller.File() - 输出一个文件(字节数组)
89          /// 返回类型为 FileContentResult
90          ///   </summary>
91          public ActionResult FileContentResult()
92          {
93              FileStream fs =   new FileStream(Request.PhysicalApplicationPath +   " Content/loading.gif " , FileMode.Open);
94              int length = ( int )fs.Length;
95              byte [] buffer =   new   byte [length];
96              fs.Read(buffer, 0 , length);
97              fs.Close();
98 
99              return   base .File(buffer, " image/gif " );
100          }
101 
102          // <summary>
103          /// Controller.File() - 输出一个文件(文件地址)
104          /// 返回类型为 FileContentResult
105          ///   </summary>
106          public ActionResult FilePathResult()
107          {
108              var path = Request.PhysicalApplicationPath +   " Content/loading.gif " ;
109              return   base .File(path, " image/gif " );
110          }
111 
112          // <summary>
113          /// Controller.File() - 输出一个文件(文件流)
114          /// 返回类型为 FileContentResult
115          ///   </summary>
116          public ActionResult FileStreamResult()
117          {
118              FileStream fs =   new FileStream(Request.PhysicalApplicationPath +   " Content/loading.gif " , FileMode.Open);
119 
120              return   base .File(fs, @" image/gif " );
121          }
122 
123          ///   <summary>
124          /// HttpUnauthorizedResult - 响应给客户端错误代码 401(未经授权浏览状态),如果程序启用了 Forms 验证,并且客户端没有任何身份票据,则会跳转到指定的登录页
125          ///   </summary>
126          public ActionResult HttpUnauthorizedResult()
127          {
128              return   new HttpUnauthorizedResult();
129          }
130 
131          ///   <summary>
132          /// Controller.PartialView() - 寻找 View ,即 .ascx 文件
133          /// 返回类型为 PartialViewResult
134          ///   </summary>
135          public ActionResult PartialViewResult()
136          {
137              return   base .PartialView();
138          }
139 
140          ///   <summary>
141          /// Controller.View() - 寻找 View ,即 .aspx 文件
142          /// 返回类型为 ViewResult
143          ///   </summary>
144          public ActionResult ViewResult()
145          {
146              // 如果没有指定 View 名称,则寻找与 Action 名称相同的 View
147              return   base .View();
148          }
149 
150          ///   <summary>
151          /// 用于演示处理 JSON 的
152          ///   </summary>
153          public ActionResult JsonDemo()
154          {
155              return View();
156          }
157 
158          ///   <summary>
159          /// 用于演示上传文件的
160          ///   </summary>
161          public ActionResult UploadDemo()
162          {
163              return View();
164          }
165 
166          ///   <summary>
167          /// 用于演示 Get 方式调用 Action
168          /// id 是根据路由过来的;param1和param2是根据参数过来的
169          ///   </summary>
170          [AcceptVerbs(HttpVerbs.Get)]
171          public ActionResult GetDemo( int id, string param1, string param2)
172          {
173              ViewData[ " ID " ] = id;
174              ViewData[ " Param1 " ] = param1;
175              ViewData[ " Param2 " ] = param2;
176 
177              return View();
178          }
179 
180          ///   <summary>
181          /// 用于演示 Post 方式调用 Action
182          ///   </summary>
183          ///   <remarks>
184          /// 可以为参数添加声明,如:[Bind(Include = "xxx")] - 只绑定指定的属性(参数),多个用逗号隔开
185          /// [Bind(Exclude = "xxx")] - 不绑定指定的属性(参数),多个用逗号隔开
186          /// [Bind] 声明同样可以作用于 class 上
187          ///   </remarks>
188          [AcceptVerbs(HttpVerbs.Post)]
189          public ActionResult PostDemo(FormCollection fc)
190          {
191              ViewData[ " Param1 " ] = fc[ " param1 " ];
192              ViewData[ " Param2 " ] = fc[ " param2 " ];
193 
194              // 也可以用 Request.Form 方式获取 post 过来的参数
195 
196              // Request.Form 内的参数也会映射到同名参数。例如,也可用如下方式获取参数 
197              // public ActionResult PostDemo(string param1, string param2)
198 
199              return View( " GetDemo " );
200          }
201 
202          ///   <summary>
203          /// 处理上传文件的 Action
204          ///   </summary>
205          ///   <param name="file1"> 与传过来的 file 类型的 input 的 name 相对应 </param>
206          [AcceptVerbs(HttpVerbs.Post)]
207          public ActionResult UploadFile(HttpPostedFileBase file1)
208          {
209              // Request.Files - 获取需要上传的文件。当然,其也会自动映射到同名参数
210              // HttpPostedFileBase hpfb = Request.Files[0] as HttpPostedFileBase;
211 
212              string targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory +   " Upload " , Path.GetFileName(file1.FileName));
213              file1.SaveAs(targetPath);
214 
215              return View( " UploadDemo " );
216          }
217      }
218  }
219 

 

你可能感兴趣的:(action)