ASP.NET ViewState is dead

UPDATE: I have left this article here for historical reasons but you should now consider ASP.NET Web Forms as obsolete due to it not supporting unit-testing and the amount of data that is passed between client and server. I recommend a modern web technology such as MVC 4 (or later) combined with javascript libraries such as jQuery and Knockout.

In my ASP.NET Web Forms / AJAX posts, I have repeatedly warned of the overhead that comes with ASP.NET postbacks and partial-postbacks. I’m talking about the very convenient but bandwidth-unfriendly mechanism for maintaining control and page state that comes free with ASP.NET Web Forms called ViewState. This is particularly noticeable when you use complex third-party controls (any developer who has used a Telerik data grid control will know what I mean) which can bloat your request and response data to ridiculous proportions because of the amount of state that is having to be maintained between browser and server.

I have seen a commercial web page developed by a contractor which was posting up to 1MB of data per request for simple user interactions with a grid control. I don’t care which way you look at it, this is just plain amateurish especially when the contractor was probably earning up to £400 per day. Page responses are slow, it eats up network bandwidth and results in a poor user experience (UX). But most importantly, ViewState breaks the stateless design of HTML over HTTP. It was simply never meant to be this way.

So I was relieved when I first starting reading about ASP.NET MVC to learn that ViewState had been dropped completely. This was one of the best design decisions made by Microsoft to rescue ASP.NET from the ashes and make it a worthy alternative to Ruby on Rails which has been promoting MVC architecture for some time. So now when you request an ASP.NET MVC page (view) and look at the page source in your browser, you will notice that it doesn’t contain kilobytes of encoded viewstate data – it just contains something rather unusual… pure, simple HTML (the novelty still hasn’t worn off).

This means your ASP.NET MVC pages are incredibly lightweight and your request and response times are much faster. It is rather ironic that at a time when most people’s homes have fast broadband, we are finally moving to a stream-lined Microsoft web technology but who’s complaining. If true separation of concerns and unit testability haven’t convinced you yet, and you need one more compelling reason to migrate your existing ASP.NET Web Form pages to MVC, then this is it.

ViewState RIP.

Advertisements

About Phil Munro

I have been developing commercial desktop and distributed web applications with Microsoft technologies since 1997.
This entry was posted in ASP.NET MVC, ASP.NET Web Forms. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s