| 饼子天空 |
|
|||
| 个人程序Blogs | ||||
|
公告
日历
统计
导航 |
最近做一个项目ASP.NET MVC2,要返回HTML数据到前台,百思苦想中,还是求助了百度 http://blog.sina.com.cn/s/blog_5f8d04170100nqwo.html 中得到了想要的答案,担心以后还会用到,把他整理到这
1 3/// EmptyResult - 空结果 4///</summary> 5public ActionResult EmptyResult() 6 { 7 Response.Write(string.Format("<span style='color: red'>{0}</span>", "EmptyResult")); 8 return new EmptyResult(); 9 } 10 11 /// <summary> 12 /// Controller.Redirect() - 转向一个指定的 url 地址 13 /// 返回类型为 RedirectResult 14 /// </summary> 15 public ActionResult RedirectResult() 16 { 17 return base.Redirect("~/ControllerDemo/ContentResult"); 18 } 19 20 /// <summary> 21 /// Controller.RedirectToAction() - 转向到指定的 Action 22 /// 返回类型为 RedirectToRouteResult 23 /// </summary> 24 public ActionResult RedirectToRouteResult() 25 { 26 return base.RedirectToAction("ContentResult"); 27 } 28 29 /// <summary> 30 /// Controller.Json() - 将指定的对象以 JSON 格式输出出来 31 /// 返回类型为 JsonResult 32 /// </summary> 33 public ActionResult JsonResult(string name) 34 { 35 System.Threading.Thread.Sleep(1000); 36 37 var jsonObj = new { Name = name, Age = new Random().Next(20, 31) }; 38 return base.Json(jsonObj); 39 } 40 41 /// <summary> 42 /// Controller.JavaScript() - 输出一段指定的 JavaScript 脚本 43 /// 返回类型为 JavaScriptResult 44 /// </summary> 45 public ActionResult JavaScriptResult() 46 { 47 return base.JavaScript("alert('JavaScriptResult')"); 48 } 49 50 /// <summary> 51 /// Controller.Content() - 输出一段指定的内容 52 /// 返回类型为 ContentResult 53 /// </summary> 54 public ActionResult ContentResult() 55 { 56 string contentString = string.Format("<span style='color: red'>{0}</span>", "ContentResult"); 57 return base.Content(contentString); 58 } 59 60 /// <summary> 61 /// Controller.File() - 输出一个文件(字节数组) 62 /// 返回类型为 FileContentResult 63 /// </summary> 64 public ActionResult FileContentResult() 65 { 66 FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open); 67 int length = (int)fs.Length; 68 byte[] buffer = new byte[length]; 69 fs.Read(buffer, 0, length); 70 fs.Close(); 71 72 return base.File(buffer, "image/gif"); 73 } 74 75 // <summary> 76 /// Controller.File() - 输出一个文件(文件地址) 77 /// 返回类型为 FileContentResult 78 /// </summary> 79 public ActionResult FilePathResult() 80 { 81 var path = Request.PhysicalApplicationPath + "Content/loading.gif"; 82 return base.File(path, "image/gif"); 83 } 84 85 86 // <summary> 87 /// Controller.File() - 输出一个文件(文件流) 88 /// 返回类型为 FileContentResult 89 /// </summary> 90 public ActionResult FileStreamResult() 91 { 92 FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif", FileMode.Open); 93 94 return base.File(fs, @"image/gif"); 95 } 96 97 /// <summary> 98 /// HttpUnauthorizedResult - 响应给客户端错误代码 401(未经授权浏览状态),如果程序启用了 Forms 验证,并且客户端没有任何身份票据,则会跳转到指定的登录页 99 /// </summary> 100 public ActionResult HttpUnauthorizedResult() 101 { 102 return new HttpUnauthorizedResult(); 103 } 104 105 106 /// <summary> 107 /// Controller.PartialView() - 寻找 View ,即 .ascx 文件 108 /// 返回类型为 PartialViewResult 109 /// </summary> 110 public ActionResult PartialViewResult() 111 { 112 return base.PartialView(); 113 } 114 115 /// <summary> 116 /// Controller.View() - 寻找 View ,即 .aspx 文件 117 /// 返回类型为 ViewResult 118 /// </summary> 119 public ActionResult ViewResult() 120 { 121 // 如果没有指定 View 名称,则寻找与 Action 名称相同的 View 122 return base.View(); 123 } 124 125 /// <summary> 126 /// 用于演示处理 JSON 的 127 /// </summary> 128 public ActionResult JsonDemo() 129 { 130 return View(); 131 } 132 133 /// <summary> 134 /// 用于演示上传文件的 135 /// </summary> 136 public ActionResult UploadDemo() 137 { 138 return View(); 139 } 140 141 /// <summary> 142 /// 用于演示 Get 方式调用 Action 143 /// id 是根据路由过来的;param1和param2是根据参数过来的 144 /// </summary> 145 [AcceptVerbs(HttpVerbs.Get)] 146 public ActionResult GetDemo(int id, string param1, string param2) 147 { 148 ViewData["ID"] = id; 149 ViewData["Param1"] = param1; 150 ViewData["Param2"] = param2; 151 152 return View(); 153 } 154 155 /// <summary> 156 /// 用于演示 Post 方式调用 Action 157 /// </summary> 158 /// <remarks> 159 /// 可以为参数添加声明,如:[Bind(Include = "xxx")] - 只绑定指定的属性(参数),多个用逗号隔开 160 /// [Bind(Exclude = "xxx")] - 不绑定指定的属性(参数),多个用逗号隔开 161 /// [Bind] 声明同样可以作用于 class 上 162 /// </remarks> 163 [AcceptVerbs(HttpVerbs.Post)] 164 public ActionResult PostDemo(FormCollection fc) 165 { 166 ViewData["Param1"] = fc["param1"]; 167 ViewData["Param2"] = fc["param2"]; 168 169 // 也可以用 Request.Form 方式获取 post 过来的参数 170 171 // Request.Form 内的参数也会映射到同名参数。例如,也可用如下方式获取参数 172 // public ActionResult PostDemo(string param1, string param2) 173 174 return View("GetDemo"); 175 } 176 177 /// <summary> 178 /// 处理上传文件的 Action 179 /// </summary> 180 /// <param name="file1">与传过来的 file 类型的 input 的 name 相对应</param> 181 [AcceptVerbs(HttpVerbs.Post)] 182 public ActionResult UploadFile(HttpPostedFileBase file1) 183 { 184 // Request.Files - 获取需要上传的文件。当然,其也会自动映射到同名参数 185 // HttpPostedFileBase hpfb = Request.Files[0] as HttpPostedFileBase; 186 187 string targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload", Path.GetFileName(file1.FileName)); 188 file1.SaveAs(targetPath); 189 190 return View("UploadDemo"); 191 } 192
程序员问答社区,解决您的IT难题
|
![]() |
|
| Copyright © 饼子天空 | Powered by: 博客园 模板提供:沪江博客 |