Application,Session,ViewState,Cookie 用法

Application: the object is used to store all the user (browser) to share data, until the Web server or pc off. 
Shared data will disappear. In fact, the application object is an application-level objects, used to share information among all users,
keep the data and can be sustained during the web application is running. At the same time, if not to limit, all clients can access the object.
Application definitions: Application [ "ViewUser"] = 0; performed after each user login
Application [ "ViewUser"] = ( int) Application [ "ViewUser"] + 1; can calculate a total number of user login.
The session : is used to indicate saved (browser) to share data, but the difference is as long as the visitors closed the dialog of the browser window, then
also put the preservation of shared data close disappeared. Session for each user is actually born, session with the user's browser will
open and create, with the disappearance of signs of life turned off or the user's browser, custom disappeared. That's the difference between them.
Session definitions:
Session [ "UserName"] = "SpringYang";
Session [ "Role"] = "the Admin";
the session memory for storing information for general less highly confidential
can save it to the user during user login names and roles, is valid only for the current user.
the ViewState: to save the state, including the page itself, then, viewstate here belongs to the state of the page itself.
valid only for the current operation page.

    public BOOL the IsNew
    {
        GET {return (BOOL) the ViewState [ "the IsNew"];}
        SET {the ViewState [ "the IsNew"] = to true;}
    }
Cookie using the client hard disk does not account for the server memory can be used to store insensitive information
the following are two ways to write their own on a cookie:
c # memory cookie
protected void setcookie ()
{
HttpCookie cookie = new new HttpCookie ( "NewValue");
cookie.value = "SpringYang"; // cookie assigned to the
DateTime time = DateTime .Now; // declare an object of a type datetime time
TimeSpan span = new new TimeSpan (1,0,0,0);
cookie.Expires = time.Add (span); // cookie expiration time span for the time of the assignment add objects time after the object
Response.Cookies.Add (cookie); // Add the cookie to the client side set of cookies in the hard disk
}
C # acquires cookie
protected void getCookie ()
{
String str=Request.Cookies["NewValue].Value;
}


Reproduced in: https: //www.cnblogs.com/springyangwc/archive/2011/02/10/1950532.html

Guess you like

Origin blog.csdn.net/weixin_33841503/article/details/93340831