MVC基础知识(传值、控制器返回方法、GET/POST请求、约定)

 1 using MVC知识点.Models;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.Mvc;
 7 
 8 namespace MVC知识点.Controllers
 9 {
10     /// <summary>
11     /// 负责演示action向视图传值的几种方式
12     /// </summary>
13     public class C01ActionPamrsController : Controller
14     {
15         public ActionResult Index()
16         {
17             #region 1.0  ViewData[""] 来传值
18 
19             ViewData["name"] = "小蛮腰";
20 
21             #endregion
22 
23             #region 2.0 TempData["age"] 来传值
24 
25             TempData["age"] = 3;
26 
27             #endregion
28 
29             #region 3.0 ViewBag.height 来传值 ,本质上还是使用ViewData["height"]来进行传值
30 
31             ViewBag.name = "八戒";
32             ViewBag.height = 118;
33 
34             #endregion
35 
36             #region 4.0 View()一般用来传递对象,如果用来传递字符串会有问题(因为View()有一个重载的参数是字符串格式的)
37             //注意:匿名类new { Name = "小白", Age = 2 }的对象实例是不能通过View()方法传入的
38             Pig pig = new Pig() { Age = 2, Name = "小黑" };
39             return View(pig); //通过View()方法传入给视图的对象,在视图中是通过@Model
40 
41             #endregion
42         }
43     }
44 }
MVC传值
  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 }
MVC控制器8种返回值(方法)
using MVC知识点.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC知识点.Controllers
{
    public class C03GetPostController : Controller
    {
        /// <summary>
        /// url:http://localhost:1176/C03GetPost/Add/300 格式请求,则自动会根据匹配的路由规则
        /// 解析出action方法和参数id,并且将id值赋值给action方法的同名参数
        /// 重点:actin方法的形参名称一定要和路由规则的参数占位符同名,否则不会自动赋值
        /// url:http://localhost:1176/C03GetPost/Add?id=300 格式请求,
        /// 则可以通过1 action方法的形参赋值 2、request,querystirng["id"]
        /// </summary>
        /// <returns></returns>
        [HttpGet] //重点 表示此方法只能被get请求,post请求不到
        public ActionResult Add(string id)
        {
            string id1 = Request.QueryString["id"];
            Response.Write("id1=" + id1 + "  ,id=" + id);

            return View();
        }

        /// <summary>
        /// 特点:获取参数可以通过action的形参
        /// 也可以通过Request.Form[]
        /// </summary>
        /// <param name="id">重点 :既可以从url中获取,也可以从请求报文体重获取
        /// 但是请求报文体中的优先级要高于url中的参数</param>
        /// <returns></returns>
        [HttpPost] //重点 表示此方法只能被Post请求,get请求不到
        public ActionResult Add(string id, FormCollection forms)
        {
            string uname = Request.Form["uname"];
            string uname1 = forms["uname"];
            Response.Write(uname + " id=" + id + " ,uname1=" + uname1);

            return View();
        }


        /// <summary>
        /// 重点
        /// MVC推荐的Post参数接收方法:
        /// 定义一个实体对象,属性名称确保和表单中的 元素 name同名
        /// 将来就会自动将表单中的元素值赋值给对象的同名属性,不区分大小写
        /// </summary>
        /// <param name="pig"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add1(Pig pig)
        {
            return Content("Name=" + pig.Name + " ,Age=" + pig.Age);
        }
    }
}
MVC(GET 与POST)请求
 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     /// <summary>
10     /// 约定:
11     /// </summary>
12     public class HomeController : Controller
13     {
14         //
15         // GET: /Home/
16         public string Index()
17         {
18             Response.Write("服务器时间=" + DateTime.Now);
19             //将字符串写入到了Resposne中 ,相当于 Resposne.Write( "服务器时间=" + DateTime.Now)
20             return "服务器时间=" + DateTime.Now;
21         }
22 
23         public ActionResult FirstView()
24         {
25             //View():作用默认去Views文件下查找和控制器同名的文件夹(Home)下再去查找和当前方法同名的视图
26             return View();
27         }
28 
29 
30         public ActionResult View1()
31         {
32             return View();
33 
34         }
35     }
36 }
MVC约定及补充

 

posted on 2015-01-08 00:14  高达  阅读(551)  评论(0)    收藏  举报

导航