Unit-III ASP - Net Intrinsic Objects and State Management
Unit-III ASP - Net Intrinsic Objects and State Management
Net
Unit-III ASP.Net Intrinsic Objects and State Management
Intrinsic Objects:
Intrinsics are built-in objects that are explicitly referenced by the algorithms of this
specification and which usually have realm-specific identities. Unless otherwise specified
each intrinsic object actually corresponds to a set of similar objects, one per realm.
ASP.NET provides intrinsic objects to enable low-level access to the Web application
framework. With the help of these intrinsic objects, you can work directly with the
underlying HTTP streams, server, session, and application objects. The intrinsic objects can
be accessed in a Web form through the properties of the Page class. Table 3.1 lists the
important intrinsic objects and the properties of the Page class to which they are mapped.
• The HttpRequest
• HTTPResponce
• HTTPServerUtility
• HTTPApplicationState
• HTTP Session state
1. The HttpRequest
The HttpRequest object . represents the incoming request from the client to the Web
server. The request from the client can come in two ways—GET or POST. GET attaches the
data with the URL, whereas POST embeds the data within the HTTP request body.
The requested page and its details are encapsulated in an HttpRequest object.
The HttpRequest intrinsic object can be accessed by the Request property of the Page class.
Tables 3.2 and 3.3 list the properties and methods of the HttpRequest class, respectively.
Because the HttpRequest class provides information about the request sent by the client, all
the properties are read-only except the Filter property.
Properties of the HttpRequest Class:
Property Description
AcceptTypes Specifies the MIME types that the client browser accepts.
Cookies Represents the cookies collection that is sent by the client to the
server.
CurrentExecutionFile Specifies the virtual path of the current executing page on the
Path Web server.
FilePath Specifies the virtual path of the file on the Web server.
Files Represents the file collection that is posted by the client to the
Web server.
Filter Represents a stream that is applied as a filter on the incoming
request.
Form Specifies the contents of a form posted to the server.
PhysicalApplicationPa Specifies the physical file system path of the application's root
th directory.
PhysicalPath Specifies the physical file system path of the current request on
the Web server.
QueryString Represents the query string collection sent by the client to the
Web server through the URL.
RawUrl Specifies the URL portion of the current request, excluding the
domain information.
RequestType Represents the type of request (GET or POST) made by the client.
TotalBytes Represents the total number of bytes posted to the server in the
current request.
Url Specifies information about the current URL request.
UrlReferrer Specifies the URL of the client's previous request that linked to
the current URL request.
UserAgent Represents the browser being used by the client.
UserHostAddress Represents the IP address of the requesting client's machine.
UserHostName Represents the Domain Name System (DNS) name of the
requesting client's machine.
UserLanguages Specifies the languages preferred by the client's browser.
2. The HttpResponse
The HttpResponse object represents the response sent back to the client from the Web
server. It contains properties and methods that provide direct access to the response stream
and allow you to set its behavior and operations. The Response property of the Page class
provides access to the HttpResponse object.
In ASP.NET, the HTTP response information is encapsulated in the HttpResponse class.
An instance of the class is created when the HTTP pipeline is set up to serve the request. The
instance is then linked to the HttpContext object associated with the request and exposed via
the Response property. The HttpResponse class defines methods and properties to manipulate
the text that will be sent to the browser. Although user-defined ASP.NET code never needs to
use the HttpResponse constructor, looking at it is still useful to get the gist of the class:
3. The HTTPServerUtility
The HttpServerUtility object contains utility methods and properties to work with the
Web server. It contains methods to enable HTML/URL encoding and decoding, execute or
transfer to an ASPX page, create COM components, and so on. The Server property of
the Page class provides access to the HttpServerUtility object. list the properties and methods
of the HttpServerUtility class, respectively.
• Syntax
public void ClearError()
Property Description
MachineName Returns the name of the server that hosts the Web application.
ScriptTimeout Indicates the number of seconds that are allowed to elapse when
processing the request before a timeout error is sent to the client.
4. The HttpApplicationState
Enables sharing of global information across multiple sessions and requests within an
ASP.NET application.
A single instance of an HttpApplicationState class is created the first time a client
requests any URL resource from within a particular ASP.NET application virtual directory. A
separate single instance is created for each ASP.NET application on a Web server. A
reference to each instance is then exposed via the intrinsic Application object.
Properties of the HttpApplicationState Class:
Property Description
IsCookieless Gets a value indicating whether the session ID is embedded in the URL
or stored in an HTTP cookie.
IsNewSession Gets a value indicating whether the session was created with the current
request.
IsReadOnly Gets a value indicating whether the session is read-only.
Methods Description
Abandon() Cancels the current session.
Add(String, Object) Adds a new item to the session-state collection.
Clear() Removes all keys and values from the session-state collection.
CopyTo(Array, Copies the collection of session-state values to a one-dimensional
Int32) array, starting at the specified index in the array.
State Management
State Management is a process by which state and page information is maintained over
multiple requests for the same or different pages. As HTTP is a stateless protocol, the server
does not store any information once the response is sent back to the client based on his
request. When user submits a request again, the server treats it as a new user. This is called
stateless model. This model was workable when static web-sites were developed and hosted
in the past. Now, with interactive web sites or dynamic web site, there is a need to preserve
some information to identify user, interact with user again and again within same session and
same application. This concept is known as Stateful protocol. The information can be related
to user, data objects, web pages or server objects.
Client-side
• View State
• Cookies
Cookies
Cookies is a small pieces of text information which is stored on user hard drive using
users browser for identify users. It may contain username, ID, password or any information.
Cookie does not use server memory.
Cookies stored user computer at “C”\Document and Setting\Current
login_User\Cookie”. Types of Cookies
1. Persistence Cookie
2. Non-Persistence Cookie
1. Persistence Cookie
This type of cookies is permanently stored on user hard drive. Cookies which have an
expiry date time are called persistence cookies. These types of cookies stored user hard drive
permanently till the date time we set.
• Example to create persistence cookie
Response.Cookies[“name”].Value = “Eagle Computers”;
Response.Cookies[“Eagle”].Expires = DateTime.Now.AddMinutes(10);
• we can also create same cookies as like below
HttpCookie strname = new HttpCookie(“name”); strname.Value = “Eagle Computers”;
strname.Expires = DateTime.Now.AddMinutes(10); Response.Cookies.Add(strname);
In the above code we use Response.Cookies object for creating Cookie. In the above
example we have set 10 Minute time for Expire Cookie, we can retrieve cookie values up
to 10 minutes, after 10 minutes the cookies automatically expire.
Prof. Manasi Talele 16
Programming Using Asp.Net
2. Non-Persistence Cookie
This types of cookies are not permanently stored on user hard drive. It stores the
information up the user accesng the same browser. When user closes the browser the cookies
will be automatically deleted.
• Example to create non-persistnce cookie
Response.Cookies[“name”].Value = “Eagle Computers”;
• we can also create same non-persistence cookies as
HttpCookie strname = new HttpCookie(“name”); strname.Value = “Eagle
Computers”; Response.Cookies.Add(strname);
• Read Cookie Information
if (Request.Cookies[“name”] != null)
{
Label1.Text = Request.Cookies[“name”].Value;
}
• ASP.Net Cookie Example
Open visual studio and design web form as shows below figure for create
cookie and retrieve cookie information.
• C# code for Cookie Example Create Cookie Button C# Code
1. Application State
2. Session State
Session state is a period of time to visit a website for a particular user. Session can
store the client data on a server. Session is a best state management features to store the
client data on server separately for each user.
• Session can store value across on multiple pages of website.
• Viewstate can store value on single page.
In simple word we can say, At the same time more than one users login to system, all
user identification name store separately until they logout. session can store username or any
unique identification of user for a login period time.
Session value can be accessible from all pages from website. We can store some
information in session in one page and can access same information on rest of all page by
using session.
• By default, Session state is enabled for all ASP.NET applications
When you login to any website with username and password. your username shows on
all other pages.The username will be stored in session and on page we access session value
to display username.
• Syntax of session
C# code for setting session value on the first page “default.aspx” on button click
events. After assigning a value to the session we redirect to other pages “next.aspx” using
response.redirect method. Here, we assign textbox name value to Session[“name”] and city
value to Session[“city”] and redirect to the next. aspx page.
protected void btnsession_Click(object sender, EventArgs e)
{
Session["name"] = txtname.Text; Session["city"] = txtcity.Text;
Response.Redirect("next.aspx");
}
Global.asax
• The Global.asax file, also known as the ASP.NET application file, is an optional file
that contains code for responding to application-level events raised by ASP.NET or by
HttpModules.
• The Global.asax file resides in the root directory of an ASP.NET-based application.
The Global.asax file is parsed and dynamically compiled by ASP.NET.
• The Global.asax file itself is configured so that any direct URL request for it is
automatically rejected; external users cannot download or view the code written within
it.
• The Global.asax file does not need recompilation if no changes have been made to it.
There can be only one Global.asax file per application and it should be located in the
application's root directory only.
• The Global.asax contains two types of events those are
• Application_EndRequest() – This event raised at the end of each request right before
the objects released.