State managament is a very important concept for server side programming technologies.
The reason is simple, the foundation of WEB, which is HTTP, is a stateless protocol.Information
exchange between server and client (GET and POST) can’t know who is the user, what
are this user’s preferences, what about his authorization? etc. so to address this
problem, server side technologies has tools to manage states between several HTTP
communications. In ASP.NET to manage to keep track of HTTP communication we have
9 choices. 4 in server side and 5 in client side and corresponding technologies
in server side and client side has many common features should be addressed accordingly.
But first of all cycle of HTTP request and where we can use state management:
1.A
web browser makes HTTP GET request (when you enter www.enestaylan.com) . Assume
that this Web browser has never visited this website or deleted all the information
of past visits.
:
2.Server (IIS or APACHE or another type) respond this request by
returning HTML to the client.Additinally .aspx page return a cookie with a unique
ID to track this specific client (Web Browser) on Set-Cookie HTTP Header.Then browser
re-returns this cookie in every subsequent HTTP call. (The state is here the cookie.Because
every cookie has unique ID and server can differentiate different clients)
:
3.Also
the developer could put some hidden text fields
|
Server-Side mechanism
|
Features
|
|
Application State
|
Fastest.
|
|
Cache Object (in Application Scope)
|
Fast.
|
|
Session State
|
Choices are:
1.In-Proc
2.Out-Of Proc
3.DB-Backed
|
|
Database
|
If several servers, can be
accessed by any server.
|
|
Client-Side mechanism
|
Features
|
|
Cookie
|
Simple.Inappropriate for sensitive
and large amounts of data.
|
|
Hidden Field
|
Fast.Inappropriate for sensitive
and large amounts of data.
|
|
View State
|
Good for page-scoped data.
|
|
Control State
|
Simple.Control specific data.
|
|
Query String
|
Simple but can be manipulated
easily on client side.
|
Let's start with session object.
SESSION OBJECT
We have 3 choices to use session object on the server side: in-process, means
in the same process in IIS, out-of-process and database backed.On the client side
we have 2 options: with cookie or with query string.Options on the server can be
manipulated like below in web.config.
<configuration>
<system.web>
<sessionState mode="Off|InProc|StateServer|SQLServer|Custom"
/>
</system.web>
</configuration>
In code we refer to session object :
Page.Session["mySession"] = "myValue";
Although Session is an attribute of Page, actually it lives in HttpContext
object and Page has just a reference to it.
In-Process Session State :
In this configuration session data is stored in the HttpRuntime's internal cache
(in IIS main process). Additinally, because in this type session objects are stored
in memory, some situations should be handled to preserve efficiency (like a session
data with 40 mb XML file)
Another problem with In-Process mode, when we change anything in our web solution
(web.config, global.asax,copying a class in \App_Code directory then our all session
states are deleted. InProc type is a simple solution
When we store items in Sesion object we use the notation:
Session["myKey"] = TextBox1.text;
To get the value of the Session object (use explicit casting):
TextBox1.text = (string)Session["mykey"];
Out-of-Process State :
This type of state is generally used in web farms, composed of many servers.
state mechanism lives in aspnet_state.exe.Generally an independent server is
dedicated to state management
SQL-BACKED Session State :
InProc offers speed, out-of-proc with an independent server resilience/speed
and SQL-BACKED session management offers resilience to even IIS starts.
Now state in client side:
Cookieless Session State :
ASP.NET uses cookies to session state on client, default.However, when we write:
<configuration>
<system.web>
<sessionState mode="InProc"
cookieless="UseUri"/>
</system.web>
</configuration>
ASP.NET stores session data on url. Like
\myapp\(S(kjdfsjdflksj35345h34hk53))\foo\k.aspx
Query Strings :
Query string is an also a tool for state management different from others,however , in a sense
that it is not for user specific but navigation specific.Example: assume that you have
a news website and a content page, which shows whichever news id comes to it:
\content.aspx?newsID=17
String id = Request.QueryString["newsID"];
if (id != null)
return Convert.ToInt32(id);
else return 0;
We get the query string field from URL and check whether it is null or not.Query
string fields are always strings so convert it to which type you need.
Hidden Fields, ViewState and ControlState :
ViewState and ControlState are generally configured for control specific.ViewState
is used like Session Object
ViewState["myView"] = "kj";
Label1.Text = ViewState["myView"];
Hidden fields as an ASP.NET control (not html control):
HiddenField1.Value = "Enes TAYLAN";
There is also another storage place, that is not used so often and lives very
short, HttpContext.Current.Items. It is used for single
HttpRequest.