ASP.NET today is widely used for high traffic web applications that 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"
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. 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. Encrypting it before sending is also costly.
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 a 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.
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:
- 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:
123456<browser refID="Default"><controlAdapters><adapter controlType="System.Web.UI.Page"adapterType="Alachisoft.NCache.Adapters.PageAdapter"/></controlAdapters></browser>
- Then add the following assembly reference in compilation section of web.config file.
1234567<compilation defaultLanguage="C#" debug="true"><assemblies><add assembly="Alachisoft.NCache.Adapters,Version=18.104.22.168,Culture=neutral,PublicKeyToken=cff5926ed6a53769"/></assemblies></compilation>
- Register your config section in web.config file.
12345678910<configSections><sectionGroup name="ncContentOptimization"><section name="settings"type="Alachisoft.NCache.ContentOptimization.Configurations.ContentSettings"allowLocation="true" allowDefinition="Everywhere"/></sectionGroup></configSections>
- Specify settings for your config section in web.config file (that was registered above).
1234567891011<ncContentOptimization><settings enableMinification="true"enableViewstateCaching="true"groupedViewStateWithSessions="true"viewstateThreshold="0"enableTrace="true"><cacheSettings cacheName="mycache"><expiration type="Sliding" duration="300"/></cacheSettings></settings></ncContentOptimization>
- In the end, register the HTTP handler in the HttpHandlers section of web.config as following:
1234567<add verb="GET,HEAD" path="NCResource.axd"validate="false"type="Alachisoft.NCache.Adapters.ContentOptimization.ResourceHandler,Alachisoft.NCache.Adapters, Version=22.214.171.124,Culture=neutral,PublicKeyToken=cff5926ed6a53769"/>
After configuring NCache, you can see the ASP.NET View State tag in your application as:
< input type="hidden"
< 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.