MVC, controls, session and cookie in general processing programs, mvcsession

Source: Internet
Author: User

MVC, controls, session and cookie in general processing programs, mvcsession

In Mvc:

Session:

If (! String. isNullOrEmpty (find) // sets Session ["oip"] = "Wuxi"; ViewBag. oip = Session ["oip"]; if (Session ["oip"] = null) // obtain Session ["oip"] = null; // set to null

Session. Timeout = 1; // set the expiration time

<SessionState mode = "InProc" timeout = "30"/> // 30 minutes after expiration

Cookie:

HttpCookie cookie = new HttpCookie ("oip"); cookie. expires = System. dateTime. now. addYears (1); cookie ["oipp"] = "username"; Response. cookies. add (cookie); // set
<SessionState mode = "InProc" timeout = "60"/> // set the expiration time
Cookie ["oipp"] = null;
HttpCookie cookiee = Response. Cookies. Get ("oip"); // Get
ViewBag. oip = cookiee ["oipp"];

Control:

Session ["UserInfo"] // set and obtain
Reference: http://www.cnblogs.com/kevin-top/archive/2010/07/04/1770726.html
Cookie: C # Calculate time difference using the timespan function http://www.blogjava.net/AndyZhang/archive/2012/05/02/377157.html
HttpCookie cookie = new HttpCookie ("MyCook"); // The Name Of The first-made Cookie and the cookie is set // DateTime dt = DateTime. now; TimeSpan ts = new TimeSpan (0, 0, 1, 0, 0); // The expiration time is 1 minute cookie. expires = dt. add (ts); // set the cookie expiration time. values. add ("userid", "userid_value"); cookie. values. add ("userid2", "userid2_value2"); Response. appendCookie (cookie );Set if (Request. Cookies ["MyCook"]! = Null) // obtain {// Response. write ("the key value of Cookie is userid:" + Request. cookies ["MyCook"] ["userid"]); // The whole line // Response. write ("the key value of Cookie is userid2" + Request. cookies ["MyCook"] ["userid2"]); Response. write (Request. cookies ["MyCook"]. value); // output all values}Get // obtain the client's Cookie object HttpCookie cok = Request. Cookies ["MyCook"]; // modify the new if (cok! = Null) {// two methods for modifying a Cookie, cok. values ["userid"] = "alter-value"; cok. values. set ("userid", "alter-value"); // Add the new content cok to the Cookie. values. set ("newid", "newValue"); Response. appendCookie (cok );}Modify and add HttpCookie cok = Request. Cookies ["MyCook"]; if (cok! = Null) {if (! CheckBox1.Checked) {cok. values. remove ("userid"); // Remove the value whose key value is userid} else {TimeSpan ts = new TimeSpan (-1, 0, 0, 0); cok. expires = DateTime. now. add (ts); // Delete the entire Cookie, as long as the expiration time is set to now} Response. appendCookie (cok );}Delete
 

General handler:

Session:

context.Session["ws_user"].ToString()Session["UserInfo"] = currentUser;

Cookie:

HttpCookie cookie = HttpContext. current. request. cookies ["info"]; // cookie = null; if (cookie = null) {cookie = new HttpCookie ("Info "); // set cookie ["CityID"] = HttpContext. current. server. urlEncode (cityID); // encode cookie ["CityName"] = HttpContext. current. server. urlEncode (CityName); cookie. expires = DateTime. now. addDays (10); // HttpContext. current. response. cookies. add (cookie);} else {// read the value directly. Note the encoding and decoding. Otherwise, garbled characters may occur.
Server. UrlDecode ()
}
# Used to mark a specific document LOCATION % 23% encode special characters % 25 & separate different variable value pairs % 26 + in the variable value, the space % 2B \ represents the directory path % 2F = used to connect the key and Value % 3D? Start % 3F of the query string

 

HttpCookie cookie = new HttpCookie ("Test"); // The name TimeSpan ts = new TimeSpan (0, 0, 1, 0, 0) of the First-made Cookie ); // The expiration time is 1 minute cookie. expires = DateTime. now. add (ts); // set the cookie expiration time. values. add ("userid", "123456"); cookie. values. add ("test", "THIS_IS_TEST"); context. response. appendCookie (cookie); context. response. write (context. request. cookies ["Test"]. value); // get

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.