ASP.NET is widely used for high-traffic web applications that handle millions of users simultaneously. Organizations deploy these applications across load-balanced web farms. One important aspect of ASP.NET is the View State, a mechanism that stores pages, controls, and custom values between multiple HTTP requests between the client and the web server. However, leveraging View State effectively, as intended, can impact performance and bandwidth.
What is ASP.NET View State?
This powerful feature tracks user input and selections on a web page, even after a reload or refresh. It allows the web application to retain user input and selections without requiring server-side sessions. View State values are stored in a hidden field on the page and encoded as a Base64 string. It looks like this:
Challenges with ASP.NET View State
While View State is incredibly useful, it can become quite large, especially in applications featuring complex controls like grid views. The increased size makes HTTP requests and responses larger, slowing down the application’s response time.
Another downside is the increased bandwidth usage which increases your bandwidth cost considerably. For instance, if each HTTP request adds 60-100 KB of View State data, multiplying this by the total number of transactions quickly escalates costs. Lastly, sending sensitive data as part of the View State poses security risks, making encryption necessary—a process that can be costly in terms of performance.
Why Cache ASP.NET View State?
To mitigate these challenges, caching ASP.NET View State on the web server and assigning a GUID as its key can be highly effective. The server sends this GUID to the browser in a hidden field, and the browser returns it with the next HTTP request. The server then uses the GUID to fetch the corresponding View State from the cache.
This approach reduces the payload sent to the browser, significantly improving response times and drastically lowering bandwidth costs.
Configuring NCache for ASP.NET View State Caching
If your ASP.NET application runs in a load-balanced web farm, you must use a distributed cache. A stand-alone cache like the ASP.NET Cache won’t work. To this end, NCache is an enterprise-level distributed cache that provides an ASP.NET View State caching module. Implementing it requires minimal code changes—just a small modification to your ASP.NET web.config file. Follow the steps below to set it up.
- Create an app.browser file in your ASP.NET application under the directory App_browsers. Add your page adapters to the app.browser file as shown below.
- Then add the following assembly reference in the compilation section of the web.config file.
- Register your config section in the web.config file.
- Specify settings for your config section in the web.config file (that was registered above).
- In the end, register the HTTP handler in the HttpHandlers section of web.config as follows.
After configuring NCache, you can see the View State tag in your application as shown below.
Notice that another hidden tag is added with ASP.NET View State. This new tag holds a unique key that links to the View State stored in your distributed cache. When the application server needs to retrieve the View State, it can quickly access it from the cache, significantly boosting performance.
Conclusion
By caching your ASP.NET View State with NCache, you can achieve remarkable improvements in your application response times while drastically reducing bandwidth costs. This simple yet effective solution significantly enhances the overall performance of your ASP.NET applications, making them more efficient and resource-effective. Explore these benefits by trying out the NCache ASP.NET View State module for yourself!
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.
https://20.200.20.123:86/storing-asp-net-session-state-in-a-microsoft-azure-distributed-cache/
Anyone successfully using Windows Azure Caching (Preview) to handle session state?