1、cookie
Continue to explain the built-in objects cookie MVC
Relatively insecure
1) Save cookie
public ActionResult Index() { // set the cookie expiration time and Response.Cookies.Add ( new new HttpCookie (name: " userId " ) { Value = "128idn62dx", Expires = DateTime.Now.AddDays(3) }); return Content("ok!"); }
2) obtain cookie
public ActionResult Index() { return Content(Request.Cookies["userId"].Value); }
3) remove the cookie
1 public ActionResult Index() 2 { 3 // 设置cookie以及过期时间 4 Response.Cookies.Add(new HttpCookie(name: "userId") 5 { 6 Expires = DateTime.Now.AddDays(-1) 7 }); 8 return Content("ok!"); 9 }
2、Application
1) is global
Settings application
1 public ActionResult Index() 2 { 3 HttpContext.Application["user"] = "Linda"; 4 return Content(HttpContext.Application["user"].ToString()); 5 }
3、Server
It contains a common method server
public ActionResult Index() { Server.Transfer(path: "html页地址"); return Content("ok"); }
Path unchanged, content changes
.MapPath virtual path to physical path turn
2, the controller (Controller) and the view (view) of a data communication
Controller inside each method accessible
1) Controller => view
①ViewBag
Controller file
1 public ActionResult Index() 2 { 3 ViewBag.Info = "info from Controller"; 4 return View(); 5 }
View file
1 @{ 2 ViewBag.Title = "Index"; 3 } 4 5 <h2>App Page for DemoController</h2> 6 <!--访问Controller内数据--> 7 8 <p>@ViewBag.Info</p>
②ViewData
public ActionResult Index() { ViewData["Name"] = "fiona"; return View(); }
@{ ViewBag.Title = "Index"; } <h2>App Page for DemoController</h2> <! - access to data within the Controller -> <p>@ViewData["Name"]</p>
③TempData
Transfer data across a page can only be accessed once, and then will be cleared
public ActionResult Index() { TempData["token"] = "23vf5c"; return View(); }
@{ ViewBag.Title = "Index"; } <h2>App Page for DemoController</h2> <! - access to data within the Controller -> <p>@TempData["token"]</p>
3 above are not the primary method of transmission of data
The main data transfer by the following method
④ Method transmitted through View
1 @{ 2 ViewBag.Title = "Index"; 3 } 4 5 <h2>App Page for DemoController</h2> 6 <!--访问Controller内数据--> 7 8 <p>@Model.Name</p> 9 <p>@Model.Sex</p>
1 public ActionResult Index() 2 { 3 return View(new Animal() 4 { 5 Name = "cat", 6 Sex = "male" 7 }); 8 }
ide can not be identified to prompt, declare
@{ ViewBag.Title = "Index"; } @model MVCStudy.Models.Animal <h2>App Page for DemoController</h2> <! - access to data within the Controller -> <p>@Model.Name</p> <p>@Model.Sex</p>
Model to type in accordance with the method parameters View
Otherwise: specifies the page and the view parameter passing
public ActionResult Index() { return View("ShowData",new Animal() { Name = "cat", Sex = "male" }); }
@{ Page.Title = "Show Title here"; // Layout = "here shows your page layout"; } @model MVCStudy.Models.Animal <div> this iss ShowData page </div> <div> data from democontroller</div> <p>@Model.Name</p> <p>@Model.Sex</p>
Specify layout templates, the following mylayout located in the shared directory
public ActionResult Index() {
// View name, page templates, data return View ( " ShowData " , masterName: " _MyLayout " , new new Animal () { Name = "cat", Sex = "male" }); }
End ------------ ------------ restore content