ASP.NET FAQ (PART II) - State Management

sanjitsil's Avatar author of ASP.NET FAQ (PART II) - State Management
This is an article on ASP.NET FAQ (PART II) - State Management in ASP.NET.
What are the different ways of state management in ASP.NET?

There are basically two ways for state management:
  1. Client side state management: ViewState, Cookies, Hiddenfield, QueryString
  2. Server side state management: Session variable, Application variable Cache can be both server side and client side.

Explain the difference between Cache object and application object.

Both the mechanisms are useful to maintain state of objects. Cache can be used for server and client side state management whereas application object is for server side state management. Application objects having scope within entire application whereas cache object having dependencies and expiration policies.

What is Cache Callback in Cache?

There are cache dependencies like file, time etc. for cache objects. The item is removed from cache when there is a change in dependency. There is an option in ASP.NET to write a callback function when the cache dependencies change.

Explain different types of Caching using Cache object of ASP.NET.

In ASP.NET 3.5 we have following types of caching:
  • Page Output Caching: Caches an entire page.
  • Partial Page Caching: Cache only particular part or region of a page.
  • DataSource Caching: The DataSource control(SqlDataSource and ObjectDataSource controls) caches the data that it represents.
  • Data Caching: We use Data Caching to cache arbitrary objects in memory, like DataSet.
How to Cache different version of same page using ASP.NET Cache object?

Following parameter is responsible to cache different version of page
  • VaryByParam
  • VaryByHeader
  • VaryByCustom
  • VaryByControl
Example:
Code:
<%@ OutputCache Duration="10000" VaryByParam="State;City" VaryByHeader="Accept-Language" VaryByCustom="browser" %>
Explain how to implement Fragment Cache.

Page fragment caching means partial page caching where cache is done for a part of page instead of the entire page. For example, cache an user control in the page.

Explain the various modes of storing ASP.NET session.

There is mainly two types: inProcess/inProc and out-Process (StateServer and Sql Server).
  • InProc: This is the default setting .In this mode Session state is stored in the worker process memory. Session state is lost on IIS or application restart.
  • StateServer: In this mode Session state is serialized and stored on a separate server.
  • Sql Server: In this mode Session state is serialized and stored in a Sql
  • Server database .Using State Server and Sql Server session state can be shared across web farms. Serialization and then desterilized increase the performance cost. SQL Server is security wise more robust but StateServer is faster as compared to SQL Server.

What are the benefits and limitations of using hidden fields?

Benefits:
  1. It is very simple to use.
  2. Stores in client side, hence no server resource is used.
  3. Supported by all browsers.
Limitations:
  1. Can be tampered.
  2. Not ideal for complex and large data storage.
What is ViewState? Explain its benefits and limitations.

Benefits:
  1. Client side state manegement and hence no server resources are required. Simple to use.
  2. States are retained automatically.
  3. The values in view state are hashed, compressed, and encoded which make view state more secure than hidden fields.
  4. Enableviewstatemac property ensure the data is not tampared.
Limitations:
  1. Page performance is effected when view state data is huge as it transfered accross the postback
  2. Data can be tampered as it stores in hiddenfields.
  3. Not ideal for sharing data between different pages.
What are benefits and limitations of using Cookies?

Benefits:
  1. Client side state manegement and hence no server resources are required. Simple to use
Limitation:
  1. Limitation in size of cookie upto 8192-bytes.
  2. Cookies can be disabled from browser which limits their use.
  3. Cookies can be tampered/deleted.
  4. Cookies can expire which cause inconsistency.

What is QueryString and what are benefits and limitations of using QueryString?

QueryString contains data which is appended at the end of page URL.

Benefits:
  1. No server resources are required as it is a part of URL in form of QueryString.
  2. Supported by all browsers.
Limitations:
  1. QueryString data is not secured as it is visible to all users and can be tampered.
  2. There is a size limitation of 255-character for URL length.
What is Absolute and Sliding expiration in .NET?

In case of time based cache dependency there is two type of expiration policy: Absolute and Sliding
In Absolute Expiration, the cache expires after a fixed time/date.
Example:
Code:
Cache.Insert("City", ds, null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration);
In Sliding Expiration the cache duration is increased by the specified sliding expiration value each time the page is accessed.
Example:
Code:
Cache.Insert("City", ds, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1));
Both Absolute and Sliding expirations can not be used at the same time.

Explain cross page posting.

Using cross-posts back technique all data and controls move from source page to the target page, the target page accesses information on the source page. We can check the cross page postback in target page using PreviousPage's property :

Code:
if(PreviousPage!=null && PreviousPage.IsCrossPagePostBack && PreviousPage.IsValid)
{
	Response.Write("Name:" + ((TextBox)(PreviousPage.FindControl("txtUName"))).Text + "<BR>");

	Response.Write("Name:" + PreviousPage.Name);

}
What is SQL Cache Dependency in ASP.NET?

SQL cache dependency is the ability to automatically invalidate a cached data object like DataSet when the related data is modified in the database.

Explain the concepts of Post Cache Substitution in .NET

Using Post Cache Substitution we can achieve the Dynamically Updating Portions of a Cached Page. Suppose there is a banner or Adrotator control which is responsible for new add for each new request. In this scenario we can dynamically update only the add part of the entire page using Post Cache Substitution.

What is ViewStateMAC?

ViewStateMAC property is used for security purpose. It is responsible to maintain data integrity of viewstate across the postbacks of page.
Using ViewStateMac it is checked whether the user has changed or tempered the viewState and made the request.
If we set the value of EnableViewStateMac to True in the @ Page directive then, the encoded and encrypted view state is checked to verify that it has not been tampered before the request made.

Describe Response.Redirect and Server.Transfer.

To perform client side redirection in ASP.NET, users can call Response.Redirect and pass the URL. When Response.Redirect is called, the server sends a command back to the browser telling it to request the page redirected to it. An extra roundtrip happens, which cause the performance hit. We can send information from the source page by using a query string. There is a limitation on the length of a query string and hence it is not ideal for transferring large amounts of data .

To perform server-side redirection, users can use Server.Transfer. Server.Transfer does not require the client to request another page. In Server.Transfer, by using HttpContext we can access the source page’s items collection in target page. It is comparatively faster than Response.Redirect. The drawback of using this method is that the browser does not know that a different page was returned to it as the URL is remained same.

Explain web garden and web farm.

Web garden is a scenario where multiple worker processes running simultaneously in a single machine. To achieve this, multiple processor or CPU is required in the machine and scheduling of the job will be handled by Windows.

On the other hand, web form is a scenario where the worker processes are running on more than one machine and all those machines are connected together.
0
nimesh's Avatar, Join Date: Apr 2009
Invasive contributor
Nice article.
0
Izaan's Avatar, Join Date: Oct 2007
Skilled contributor
Nice collection of FAQs
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nomination this Article for Article of the month - May 2009
0
omiban's Avatar, Join Date: Jun 2009
Newbie Member
Every ASP.NET application needs to manage client session information because of stateless nature of HTTP protocol. A question that is often asked in newsgroups, forums, and mailing lists is that, how does ASP.NET Session Management work, and which technique in ASP.NET is the best way to manage session in a web farm or high volume web site with large number of concurrent users? We will begin with a quick overview of ASP.NET session management alternatives, then dive into ASP.NET Session Management internals, and make some recommendations at the end.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by omiban View Post
We will begin with a quick overview of ASP.NET session management alternatives, then dive into ASP.NET Session Management internals, and make some recommendations at the end.
I dont see it ?
0
arslan220's Avatar, Join Date: Mar 2007
Go4Expert Member
nice article.