Home > front end >  The Session with Cookies, viewBag and codebehind (categories.aspx.cs)
The Session with Cookies, viewBag and codebehind (categories.aspx.cs)

Time:12-01

1, the Session with Cookies
Abstract: due to network requests are stateless, Session and a Cookie is used to solve this problem,
Principle: the browser requests the server for the first time, the server will open up an area in the memory to
Create a Session, used to hold conversations with current browsers, in creating the Session with
For the distribution of the Session when a SessionID, response to the browser's request and at the same time the SessionID
Back to the browser, the browser will be saved in Cookies SessionID, from the second and subsequent
Requests will be carry corresponding SessionID, thus ensures the consistency of the request and response,
The Session:
Save: the Session [" key "]=data;
Key: name of the data stored in the Session, it needs to be obtain corresponding to obtain the name of the
Data, the data that represents the need to save data
Access: var data=https://bbs.csdn.net/topics/Session (" key ");
Note: all saved to the Session data into object types of data, so get to
Must carry on the data type conversion
Example: the string name="* *";//data
Sesssion [" name "]=name;//save data
String name=Session [r]. "the name" the toString ();//get data
Cookie:
Save://1, declare a Cookie object, it needs to be set the name, obtain corresponding to obtain the name of the
HttpCookie cookie=new HttpCookie (" test ");
//2, save the data
String name="bill";
Cookies/" name "=name;
//3, the validity of the setting cookies (according to the actual situation, set up)
Cookies. Expries=DateTime. Now. AddDays (1);
//4, through the response to the statement of Cookies add (return) to the browser Cookies in
Response. Cookies. The Add (Cookies);
Access://1, from the request to carry the cookies, need for cookies
HttpCookie cookie=System. Web. HttpContext. Current. Request. Cookies (" test ");
//2, whether for cookie is empty
String name="";
If (cookies!=null) {
//3, to get the data, because the Cookie can only save string types of data, so the stored in the
String may be encoded in a Cookie, so to get the data needed solution
Code, not directly get
Name=System. Web. HttpContext. Current. Server UrlDeCode (cookies/" name ");
}

2, the codebehind (categories.aspx.cs) and viewBag
Note: the codebehind (categories.aspx.cs) and viewBag only in return to view method can use the
The codebehind (categories.aspx.cs) :
Save: string name="zhang SAN";
The codebehind (categories.aspx.cs) [" name "]=name;
Access: @ the codebehind (categories.aspx.cs) [r]. "the name" the toString ();
Instructions: save the data in the codebehind (categories.aspx.cs) will become the object types of data, so the need when get the data
To convert the data types,
ViewBag:
Save: string name="bill";
ViewBag. Name=name;
Access: @ viewBag. Name;
Instructions: save the data in a viewBag, save time data types are what is data types,
To get the data do not need to carry on data type conversion,

CodePudding user response:

  • Related