#region 1.0 ContentResult - 返回字符串 Response.Write(""); /// <summary> /// 1.0 ContentResult /// </summary> /// <returns></returns> public ActionResult ContentStr() { //1.调用 控制器类 提供的便捷方法 创建 ContentResult对象 //return Content("我是字符串~~你懂吗?"); //2.手动创建 ContentResult 对象 return new ContentResult() { Content = "<h1>小白~~我懂你!</h1>", ContentType = "text/html", ContentEncoding = System.Text.Encoding.Default }; } #endregion
#region 2.0 EmptyResult /// <summary> /// 2.0 EmptyResult /// </summary> /// <returns></returns> public ActionResult EmptyResult() { return new EmptyResult(); } #endregion
#region 3.0 FileResult - 输出文件流 public ActionResult FileResult() { byte[] arrFile = null; using (Image img = new Bitmap(90, 30)) { using (Graphics g = Graphics.FromImage(img)) { g.DrawString("我是验证码", new Font("微软雅黑", 12), Brushes.White, 1, 1); } //img.Save(Response.OutputStream); //将图片 装入 内存流 using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //将 内存流数据 转成 byte数组 arrFile = ms.ToArray(); } } return File(arrFile, "image/jpeg"); //return new FileContentResult(arrFile, "image/jpeg"); } #endregion
#region 4.0 指定 输出 状态码 错误页 - HttpStatusCodeResult /// <summary> /// 4.0 指定 输出 状态码 错误页 /// </summary> /// <returns></returns> public ActionResult HttpStatus() { return new HttpStatusCodeResult(500); } #endregion
#region 5.0 JavaScript 返回 js 代码,只能 针对 浏览器 script标签 时,才执行 /// <summary> /// JavaScript 返回 js 代码,只能 针对 浏览器 script标签 时,才执行 /// </summary> /// <returns></returns> public ActionResult JSResult() { return JavaScript("alert('小白来了~~~~~~~!');"); } #endregion
#region 6.0 JsonResult-内部就是使用 JavaScriptSerializer 将对象 序列化成 json字符串 /// <summary> /// JsonResult-内部就是使用 JavaScriptSerializer 将对象 序列化成 json字符串 /// </summary> /// <returns></returns> public ActionResult Json2() { Models.BlogArticleCate cate = new Models.BlogArticleCate() { Id = 1, Name = "7期班", Remark = "7期班备注", Addtime = DateTime.Now, IsDel = false, Statu = 4, BlogUser = new Models.BlogUser() { Id = 2, LoginName = "xiaobai" } }; //如果针对 get 请求,需要通过 第二个参数 设置;因为默认 Json方法拒绝 get请求。 return Json(cate, JsonRequestBehavior.AllowGet); } #endregion
#region 7.0 RedirectResult - 内部就是使用:Response.Redirect(url, flag2); /// <summary> /// RedirectResult - 内部就是使用:Response.Redirect(url, flag2); /// </summary> /// <returns></returns> public ActionResult RedirectTo() { return Redirect("/home/index"); } #endregion