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:
- Then add the following assembly reference in compilation section of web.config file.
<compilation defaultLanguage="C#" debug="true">
- Register your config section in web.config file.
- Specify settings for your config section in web.config file (that was registered above).
<expiration type="Sliding" duration="300"/>
- In the end, register the HTTP handler in the HttpHandlers section of web.config as following:
<add verb="GET,HEAD" path="NCResource.axd"
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.