Information will be stored on the server, it has higher security but it can use more web server resources.
A. Aplication object
The Application object provides a mechanism for storing data that is accessible to all code running within the Web application, The ideal data to insert into application state variables is data that is shared by multiple sessions and does not change often.. And just because it is visible to the entire application, you need to used Lock and UnLock pair to avoid having conflit value.
[c#]
Application.Lock();
Application[“mydata”]=”mydata”;
Application.UnLock();
B. Session object
Session object can be used for storing session-specific information that needs to be maintained between server round trips and between requests for pages. Session object is per-client basis, which means different clients generate different session object.The ideal data to store in session-state variables is short-lived, sensitive data that is specific to an individual session.
Each active ASP.NET session is identified and tracked using a 120-bit SessionID string containing URL-legal ASCII characters. SessionID values are generated using an algorithm that guarantees uniqueness so that sessions do not collide, and SessionID’s randomness makes it harder to guess the session ID of an existing session.SessionIDs are communicated across client-server requests either by an HTTP cookie or a modified URL, depending on how you set the application's configuration settings. So how to set the session setting in application configuration? Ok, let’s go further to look at it.
Every web application must have a configuration file named web.config, it is a XML-Based file, there is a section name ‘sessionState’, the following is an example:
cookieless’ option can be ‘true’ or ‘false’. When it is ‘false’(default value), ASP.NET will use HTTP cookie to identify users. When it is ‘true’, ASP.NET will randomly generate a unique number and put it just right ahead of the requested file, this number is used to identify users, you can see it on the address bar of IE:
http://localhost/Management/(2yzakzez3eqxut45ukyzq3qp)/Default.aspx
Ok, it is further enough, let is go back to session object.
[c#]
//to store information
Session[“myname”]=”Mike”;
//to retrieve information
myname=Session[“myname”];
C. Database
Database enables you to store large amount of information pertaining to state in your Web application. Sometimes users continually query the database by using the unique ID, you can save it in the database for use across multiple request for the pages in your site.
Summary
ASP.NET has more functions and utilities than ASP to enable you to manage page state more efficient and effective. Choosing among the options will depand upon your application, you have to think about the following before making any choose:
How much information do you need to store?
Does the client accept persistent or in-memory cookies?
Do you want to store the information on the client or server?
Is the information sensitive?
What kind of performance experience are you expecting from your pages?
Client-side state management summary
Method Use when
Cookies You need to store small amounts of information on the client and security is not
an issue.
View state You need to store small amounts of information for a page that will post back to
itself. Use of the ViewState property does supply semi-secure functionality.
Hidden fields You need to store small amounts of information for a page that will post back to
itself or another page, and security is not an issue.
Note You can use a hidden field only on pages that are submitted to the server.
Query string You are transferring small amounts of information from one page to another and
security is not an issue.
Note You can use query strings only if you are requesting the same page, or another page via a link.
Server-side state management summary
Method Use when
Application state object You are storing infrequently changed, application-scope information
that is used by many users, and security is not an issue. Do not store
large quantities of information in an application state object.
Session state object You are storing short-lived information that is specific to an individual
session, and security is an issue. Do not store large quantities of
information in a session state object. Be aware that a session state object
will be created and maintained for the lifetime of every session in your
application. In applications hosting many users, this can occupy
significant server resources and affect scalability.
Database support You are storing large amounts of information, managing transactions, or
the information must survive application and session restarts. Data mining
is a concern, and security is an issue.
Developing ASP.NET programme is really funny, once you jump into it, you can feel the power of it. Next time let us talk about another topic: Cache. Enjoy .Net!!
No comments:
Post a Comment