1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; using System.IO; namespace MVC.Controllers { /// <summary> /// Controller 类必须以字符串 "Controller" 做类名称的结尾,字符串 Controller 之前的字符串为 Controller 的名称,类中的方法名为 Action 的名称 /// </summary> public class ControllerDemoController : Controller { // [NonAction] - 当前方法仅为普通方法,不解析为 Action // [AcceptVerbs(HttpVerbs.Post)] - 声明 Action 所对应的 http 方法 /// <summary> /// Action 可以没有返回值 /// </summary> public void Void() { Response.Write( string .Format( "<span style='color: red'>{0}</span>" , "void" )); } /// <summary> /// 如果 Action 要有返回值的话,其类型必须是 ActionResult /// EmptyResult - 空结果 /// </summary> public ActionResult EmptyResult() { Response.Write( string .Format( "<span style='color: red'>{0}</span>" , "EmptyResult" )); return new EmptyResult(); } /// <summary> /// Controller.Redirect() - 转向一个指定的 url 地址 /// 返回类型为 RedirectResult /// </summary> public ActionResult RedirectResult() { return base .Redirect( "~/ControllerDemo/ContentResult" ); } /// <summary> /// Controller.RedirectToAction() - 转向到指定的 Action /// 返回类型为 RedirectToRouteResult /// </summary> public ActionResult RedirectToRouteResult() { return base .RedirectToAction( "ContentResult" ); } /// <summary> /// Controller.Json() - 将指定的对象以 JSON 格式输出出来 /// 返回类型为 JsonResult /// </summary> public ActionResult JsonResult( string name) { System.Threading.Thread.Sleep(1000); var jsonObj = new { Name = name, Age = new Random().Next(20, 31) }; return base .Json(jsonObj); } /// <summary> /// Controller.JavaScript() - 输出一段指定的 JavaScript 脚本 /// 返回类型为 JavaScriptResult /// </summary> public ActionResult JavaScriptResult() { return base .JavaScript( "alert('JavaScriptResult')" ); } /// <summary> /// Controller.Content() - 输出一段指定的内容 /// 返回类型为 ContentResult /// </summary> public ActionResult ContentResult() { string contentString = string .Format( "<span style='color: red'>{0}</span>" , "ContentResult" ); return base .Content(contentString); } /// <summary> /// Controller.File() - 输出一个文件(字节数组) /// 返回类型为 FileContentResult /// </summary> public ActionResult FileContentResult() { FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif" , FileMode.Open); int length = ( int )fs.Length; byte [] buffer = new byte [length]; fs.Read(buffer, 0, length); fs.Close(); return base .File(buffer, "image/gif" ); } // <summary> /// Controller.File() - 输出一个文件(文件地址) /// 返回类型为 FileContentResult /// </summary> public ActionResult FilePathResult() { var path = Request.PhysicalApplicationPath + "Content/loading.gif" ; return base .File(path, "image/gif" ); } // <summary> /// Controller.File() - 输出一个文件(文件流) /// 返回类型为 FileContentResult /// </summary> public ActionResult FileStreamResult() { FileStream fs = new FileStream(Request.PhysicalApplicationPath + "Content/loading.gif" , FileMode.Open); return base .File(fs, @"image/gif" ); } /// <summary> /// HttpUnauthorizedResult - 响应给客户端错误代码 401(未经授权浏览状态),如果程序启用了 Forms 验证,并且客户端没有任何身份票据,则会跳转到指定的登录页 /// </summary> public ActionResult HttpUnauthorizedResult() { return new HttpUnauthorizedResult(); } /// <summary> /// Controller.PartialView() - 寻找 View ,即 .ascx 文件 /// 返回类型为 PartialViewResult /// </summary> public ActionResult PartialViewResult() { return base .PartialView(); } /// <summary> /// Controller.View() - 寻找 View ,即 .aspx 文件 /// 返回类型为 ViewResult /// </summary> public ActionResult ViewResult() { // 如果没有指定 View 名称,则寻找与 Action 名称相同的 View return base .View(); } /// <summary> /// 用于演示处理 JSON 的 /// </summary> public ActionResult JsonDemo() { return View(); } /// <summary> /// 用于演示上传文件的 /// </summary> public ActionResult UploadDemo() { return View(); } /// <summary> /// 用于演示 Get 方式调用 Action /// id 是根据路由过来的;param1和param2是根据参数过来的 /// </summary> [AcceptVerbs(HttpVerbs.Get)] public ActionResult GetDemo( int id, string param1, string param2) { ViewData[ "ID" ] = id; ViewData[ "Param1" ] = param1; ViewData[ "Param2" ] = param2; return View(); } /// <summary> /// 用于演示 Post 方式调用 Action /// </summary> /// <remarks> /// 可以为参数添加声明,如:[Bind(Include = "xxx")] - 只绑定指定的属性(参数),多个用逗号隔开 /// [Bind(Exclude = "xxx")] - 不绑定指定的属性(参数),多个用逗号隔开 /// [Bind] 声明同样可以作用于 class 上 /// </remarks> [AcceptVerbs(HttpVerbs.Post)] public ActionResult PostDemo(FormCollection fc) { ViewData[ "Param1" ] = fc[ "param1" ]; ViewData[ "Param2" ] = fc[ "param2" ]; // 也可以用 Request.Form 方式获取 post 过来的参数 // Request.Form 内的参数也会映射到同名参数。例如,也可用如下方式获取参数 // public ActionResult PostDemo(string param1, string param2) return View( "GetDemo" ); } /// <summary> /// 处理上传文件的 Action /// </summary> /// <param name="file1">与传过来的 file 类型的 input 的 name 相对应</param> [AcceptVerbs(HttpVerbs.Post)] public ActionResult UploadFile(HttpPostedFileBase file1) { // Request.Files - 获取需要上传的文件。当然,其也会自动映射到同名参数 // HttpPostedFileBase hpfb = Request.Files[0] as HttpPostedFileBase; string targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload" , Path.GetFileName(file1.FileName)); file1.SaveAs(targetPath); return View( "UploadDemo" ); } } } |
转自:http://Www.CnBlogs.Com/WebEnh/
如果想下次快速找到我,记得点下面的关注哦!
本博客Android APP 下载 |
![]() |
支持我们就给我们点打赏 |
![]() |
支付宝打赏 支付宝扫一扫二维码 |
![]() |
微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!