How to Cache ASP.NET View State in a Distributed Cache?

ASP.NET today is widely used for high traffic web applications that can need to handle millions of users and are deployed in load balanced web farms. One important part of ASP.NET is View State that many applications use. ASP.NET View State is a very powerful mechanism that stores pages, controls and custom values between multiple HTTP requests across client and the web server.

ASP.NET View State values are stored in a hidden field on the page and encoded as a Base64 string. An ASP.NET View State looks like this:

<input id="__VIEWSTATE" type="hidden"
name="__VIEWSTATE"
value="/wEPDwUJNzg0MDMxMDA1D2QWAmYPZBYCZg9kF..." />

Although very useful, ASP.NET View State frequently becomes quite large especially in situations where your ASP.NET application has grid controls and many other controls on pages. And, this is added to your HTTP request and response and that really slows down your ASP.NET response time to unbearable levels.

Another downside of heavy ASP.NET View State is the increased bandwidth usage that increases your bandwidth cost considerably. For example, if for each HTTP requests, you end up appending 60-100k of additional ASP.NET View State data, just multiply it by the total number of transactions and you’ll quickly see how much more it will cost you in bandwidth consumption.

Finally, in some situations, there is a security risk with sending confidential data as part of ASP.NET View State. And, encrypting it before sending is also costly.

Download NCache free trial - Extremely fast and scalable in-memory distributed cache

To resolve all these problems you can cache ASP.NET View State on the web server and assign a GUID as its key in the cache. This GUID is then sent to the browser in hidden field and it comes back along with the next HTTP request and is used to fetch the corresponding ASP.NET View State from the cache. This reduces your payload sent to the browser which not only improves ASP.NET response time but also reduces your bandwidth consumption cost dramatically.

And, if your ASP.NET application is running in a load balanced web farm then you must use a distributed cache. A stand-alone cache like ASP.NET Cache won’t work. NCache is an enterprise level distributed cache that provides ASP.NET View State caching module. In order to use it, you don’t need to do any programming. Just modify your ASP.NET web.config for it.

Here are the steps to use NCache for caching ASP.NET View State:

  1. Create an app.browser file in your ASP.NET application. Create it under the directory App_browsers. Plug in page adapters in the app.browser file as following:
    <browser refID="Default">
    <controlAdapters>
    <adapter controlType="System.Web.UI.Page"
     adapterType="Alachisoft.NCache.Adapters.PageAdapter"/>
    </controlAdapters></browser>
    
  2. Then add the following assembly reference in compilation section of web.config file.
    
    <compilation defaultLanguage="C#" debug="true">
    <assemblies>
    <add assembly="Alachisoft.NCache.Adapters,
    Version=1.0.0.0,Culture=neutral,
    PublicKeyToken=cff5926ed6a53769"/></assemblies>
    </compilation>
    
  3. Register your config section in web.config file.
    <configSections>
    <sectionGroup name="ncContentOptimization">
    <section name="settings"
    type=
    "Alachisoft.NCache.ContentOptimization
    .Configurations.ContentSettings"
    allowLocation="true" allowDefinition="Everywhere"/>
    </sectionGroup>
    </configSections>
    
    
  4. Specify settings for your config section in web.config file (that was registered above).
    <ncContentOptimization>
    <settings enableMinification="true"
    enableViewstateCaching="true"
    groupedViewStateWithSessions="true"
    viewstateThreshold="0"
    enableTrace="true">
    <cacheSettings cacheName="mycache">
    <expiration type="Sliding" duration="300"/>
    </cacheSettings></settings>
    </ncContentOptimization>
    
  5. In the end, register the HTTP handler in the HttpHandlers section of web.config as following:
    <add verb="GET,HEAD" path="NCResource.axd"
     validate="false"
    type="Alachisoft.NCache.Adapters
    .ContentOptimization.ResourceHandler,
    Alachisoft.NCache.Adapters, Version=1.0.0.0,
    Culture=neutral,PublicKeyToken=cff5926ed6a53769"/>
    

After configuring NCache, you can see the ASP.NET View State tag in your application as:

< input type="hidden"
name="__NCPVIEWSTATE"
id="__NCPVIEWSTATE"
value="vs:cf8c8d3927ad4c1a84da7f891bb89185" />
< input type="hidden"
name="__VIEWSTATE" id="__VIEWSTATE" value="" />

Notice that another hidden tag is added with ASP.NET View State. It contains the unique key assigned to ASP.NET View State of your page, in your distributed cache. So whenever your application server needs ASP.NET View State value it can get it from cache easily.

With this, you will see a remarkable performance boost in your ASP.NET response times and your bandwidth consumption cost is reduced significantly as well.

Please explore more about ASP.NET View State caching by trying NCache ASP.NET View State module yourself.

Download NCache Trial | NCache Details

This entry was posted in ASP .NET performance, ASP.Net, ASP.NET Cache, Distributed Cache, Distributed caching and tagged , , . Bookmark the permalink.

2 Responses to How to Cache ASP.NET View State in a Distributed Cache?

  1. Anyone successfully using Windows Azure Caching (Preview) to handle session state?

  2. Iqbal Khan says:

    You can use NCache for Azure for storing ASP.NET Session State for Microsoft Azure ASP.NET applications. Here is a blog that explains Microsoft Azure Session Storage options and why NCache for Azure is best among all available options in Microsoft Azure.

    http://blogs.alachisoft.com/ncache/storing-asp-net-session-state-in-a-microsoft-azure-distributed-cache/

Leave a Reply

Your email address will not be published. Required fields are marked *


6 − = one

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>