Optimize ASP.NET Performance With View State Caching
Learn about the ASP.NET feature ASP.NET View State and how to avoid performance and security issues by caching your ASP.NET View State in NCache.
Join the DZone community and get the full member experience.Join For Free
asp.net view state is a very powerful feature of asp.net that provides a client-side state management mechanism. it helps preserve page and control values between complete round trips for client requests. this gives a state full programming capability over a stateless protocol such as http.
asp.net view state is stored in a hidden field on the page as an encoded base64 string, as part of every response sent to the client and is also returned to the server by the client as part of a post back.
<input id="__viewstate" type="hidden" name="__viewstate" value="/wepdwujnzg0mdmxmda1d2qwamypzbyczg9kfgqcaq9kfgicbq9kfgjmd2qwagibd xychhnqcm2aw91c0nvbnryb2xnb2rlcymiau1py3jvc29mdc5tagfyzvbvaw50lld lyknvbnryb2xzllnqq29udhjbe1vzda1xzrlmjjfodm3y19kowq1ztc2ymy1m2ipd 2…==" />
problems with asp.net view state
asp.net view state does come with a few issues that you need to understand and resolve. once you resolve these issues, you are able to benefit from asp.net view state without any problems. they are discussed below.
asp.net view state is often heavy
first of all, in many situations, asp.net view state becomes very large especially when your asp.net application has a lot of rich and heavy controls and widgets on its pages. this results in a lot of data traveling back and forth between your browser and the web server.
due to this heavy payload, your asp.net performance drops because your pages are taking longer to respond. this is probably the most visible change you see in your asp.net application.
another impact is the extra bandwidth consumption. this has a financial cost associated with it because you're paying for the bandwidth and if an average asp.net view state ends up in 100's of kilobytes, this could easily consume a lot of bandwidth when millions of requests are processed.
asp.net view state is a security risk
asp.net view state can also pose a security risk when sending confidential data as part of view state to the client. this data is vulnerable to attacks and can be tempered by an attacker which is a serious security threat. you can encrypt the asp.net view state data but this is again going to have a performance cost to it.
solution to asp.net view state problems
one way you can resolve asp.net view state issues is by storing the actual asp.net view state on the web server and sending a unique token (or id) in place of it to the browser so the browser can send this token back to the web server next time. the web server then uses this token to find the right asp.net view state in its store.
you can do this because the asp.net view state encoded string is never used by the browser and is always returned to the web server. so, whether it is an encoded string or a token is the same for the browser. below is an example of a token being used in place of asp.net view state.
<input type="hidden" name="__viewstate" id="__viewstate" value="vs:cf8c8d3927ad4c1a84da7f891bb89185" />
but, if your asp.net application is running in a load balanced web farm, the next http request might come to another web server. therefore, you must store the asp.net view state in a shared store that is accessible from all the web servers.
the best place to store asp.net view state on the server is in a distributed cache. this way, not only can you have a common store for all web servers, but you also have an extremely fast and scalable in-memory store as compared to sql server database or other storage options.
figure 1: distributed cache storing asp.net view state.
ncache is an extremely fast and scalable distributed cache for .net. it also lets you store asp.net view state to solve the issues described above.
create an app.browser file in app_browsers directory. plug in page adapters in the app.browser file as follows:
<browser refid="default"> <controladapters> <adapter controltype="system.web.ui.page" adaptertype="alachisoft.ncache.adapters.pageadapter" /> </controladapters> </browser>
add the following assembly reference in compilation section of web.config file.
<compilation defaultlanguage="c#" debug="true" targetframework="4.0"> <assemblies> <add assembly="alachisoft.ncache.adapters, version=184.108.40.206, culture=neutral, publickeytoken=cff5926ed6a53769"/> </assemblies> </compilation>
register ncache config section in your web.config file.
<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). these settings control ncache asp.net view state caching features.
<nccontentoptimization> <settings viewstatethreshold="12" enableviewstatecaching="true" enabletrace="false" groupedviewstatewithsessions="false" maxviewstatespersession="3" > <cachesettings cachename="mycache"> <expiration type="absolute" duration="1" /> </cachesettings> </settings> </nccontentoptimization>
in the end, register the http handler in the httphandlers section of web.config as following:
<httphandlers> <add verb="get,head" path="ncresource.axd" validate="false" type="alachisoft.ncache.adapters.contentoptimization.resourcehandler, alachisoft.ncache.adapters, version=220.127.116.11, culture=neutral, publickeytoken=cff5926ed6a53769"/> </httphandlers>
benefits of caching asp.net view state on the server
you gain the following benefits by caching your asp.net view state in ncache.
- improve asp.net performance: a small token is now sent to the browser instead of 100's of kilobytes of view state data. this reduces the payload size and improves performance.
- reduce bandwidth cost: smaller payload also means a significant reduction in bandwidth consumption cost. this could save you a lot of money.
- security: now that asp.net view state encoded string is not sent to the browser, there is no longer any security risks.
- fast & scalable asp.net view state storage: ncache is an extremely fast and scalable distributed cache. this means your asp.net never faces any scalability bottlenecks due to asp.net view state storage.
- asp.net view state reliability thru replication: ncache replicates all data in the distributed cache intelligently. this means you don't lose any asp.net view state even if a cache server goes down.
advanced asp.net view state caching features in ncache
ncache provides you a rich set of features for caching and managing asp.net view state. below is a list of them.
- minimum size threshold: ncache allows you to specify a minimum size of asp.net view state. any view state smaller than this is not cached. this enables you to only cache heavy view state.
- link asp.net view state with session state: you can link an asp.net view state with your session state. this way, when the user session expires, all his view states are automatically removed from the cache.
- page level max count threshold: you can configure how many view states should be kept for a given page in a fifo manner. this way, the oldest view state is removed whenever a new view state is created for this page. this optimizes your memory consumption in the cache server by not caching view states that you would never need.
- session level max count threshold: similar to page level, you can specify maximum view state count for a user session in a fifo manner. this way, the oldest view state is removed whenever a new view state is created.
- page level and session level settings: you can specify all of the above settings differently for each page or keep them common to all sessions.
as you have seen, ncache allows you to cache asp.net view state on the server in order to optimize your asp.net performance. additionally, ncache provides you a rich set of features on managing your asp.net view state more efficiently in the cache. this allows you to develop complex applications and use these features to handle a variety of scenarios.
Opinions expressed by DZone contributors are their own.