Scott Hanselman

ASP.NET ViewState: Pox on mankind? Or clever like a fox?

February 23, 2004 Comment on this post [3] Posted in ASP.NET | Javascript | ViewState | HttpModule
Sponsored By

A good friend of mine (who may or may not reveal himself by trackbacking this post) emailed me recently, with reference to my post on ASP.NET ViewState. 

He had some interesting opinions, so here are some choice snippets reprinted with this permission, with my commentary interlaced:

IMHO viewstate is the second worst piece of the .NET Framework. The worst being "javascript:do_postback" instead of providing clean, lean and mean URLs. Oh wait, they are related -- the latter is the necesary workaround for the former.

I think it's actually damned clever, and quite possibly neccesary.  The HTTP/HTML combination needed an eventing subsystem built on top of it.  DoPostback() does just that with support for Event Targets and Event Arguments.  It's simple, supported, and clean.  POSTing data (rather than GETting) is needed, nay, required to move stuff from place to place on browser-based web.  I certainly can't be putting on this crap in my URLs, can I?  Believe me, I'm all about REST, and I believe the REAL answer lies in both POSTs, and well designed URI/Ls.

Viewstate is bad because I can't set a bookmark to it. Instead of using viewstate, developers who want to create great webapps should pass a querystring parameter for read-only operations. The whole concept of doPostback should be abandoned altogether and developers should instead look up the original HTTP specification which details the ramification of GET vs. POST.

Phooey! :)  Viewstate was needed to allow things like listboxes to POST not just their currently selected item, but also ALL the other items.  Otherwise, how do I reconsitute my listbox on the next go-around?  Better I pay a few BASE64'ed bytes on the roundtrip than head back to the database.

ViewState was just a state bag, and as Brad notes in the comments of the previous post, it can be "normalized" away with a key (I wonder if they are hearing this and will hook it up to ASP.NET Session State in Whidbey?)

Whenever a certain resource is only addressed (and not changed in any way, i.e. whenever the underlying operation is safe), GET should be used so that you allow the user to have a uniform resource locator for this very thing. Just imagine, google.com would use viewstate and such ... you wouldn't be able to address the query for "Scott Hanselman" as http://www.google.com/search?q=%22scott%20hanselman%22. Or the second page on this query as http://www.google.com/search?q=%22scott+hanselman%22&num=100&hl=en&lr=&ie=UTF-8&start=100&sa=N. Now, that would be bad.

Now this I totally agree with.  GETs and "hackable URLs" are totally appropriate for an operation like this.  But for a form that includes my credit card info and full address, not so.  Then add in dropdowns and database-backed grids, then maybe a few buttons...aren't you wishing you had an eventing subsystem and a way to reconstitute your controls? ;)

Second reason for why this whole thing (especially javascript:dopostback) is bad: It doesn't allow me to "Open in new window". I use a tabbed browser and regularily open links in new windows. I find more and more sites which just use ASP.NET and don't handcode their URLs anymore leading to "forced-single-window-navigation."

True, I hate that also.  But, to be clear, my mom, and most folks on the web, don't care.

 But let's get back to the topic: the creation of viewstate and the associated necessity for doPostback instead of <a href=""> have created a bunch of non-standard, non-userfriendly web applications.

Unfriendly is up for debate, but non-standard?  I don't think so.  Last time I checked I had ASP.NET sites up running on 15 different browser variants using doPostback happily and transparently.  If there's a better way that will still give me:

  • Server-side Events (onclick, onchange, etc)
  • Reconstitutes the state of my controls without requiring a call back to the datastore.

Then I'll be the first to code the HttpModules or whatever's required to make it happen.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

The Myth, The Legend, the Interop - Using the .NET Framework SDK Interoperability Tools

February 23, 2004 Comment on this post [0] Posted in Programming | Tools
Sponsored By

Sam Gentile's second COM Interop article on "Using the .NET Framework SDK Interoperability Tools" is up on MSDN!

Kudos to Sam for another well written and easy to read (while the subject is complex) article.  Now I have yet another place to point folks for Interop Zen.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Scott Hanselman's .NET Rocks show is up and ready for download!

February 23, 2004 Comment on this post [0] Posted in ASP.NET
Sponsored By

My .NET Rocks show (that was Live last week) is available for download.  Carl and Rory banter for a while, and I show up at about 0:22 minutes in and for the rest of the show.

Also, check out Peter Blum at about 0:55, he talks about his line of products that I wholly recommend!

Definitely a show worth iPod'ing or burning to a CD.  Also, it'll be up on MSDN probably tomorrow.

Thanks to Rory and Carl and all their Audio folks for making their 2nd Live show, while a little dodgy during the live streaming broadcast, a total BLAST to be on.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Here's a great list of tips on Optimzing Performance with Virtual PC (VPC)

February 19, 2004 Comment on this post [6] Posted in Programming
Sponsored By

Here's a great list of tips from my buddy J. Sawyer at MSFT.

Biggest perf tip is to put the VPC Virtual Hard Disks (VHD) to separate disk spindles from the operating system.  The biggest perf issue with VPC is related to disk I/O … and by making the VPC fight with your OS and swap disk make this issue much, much worse.  Additionally, today’s USB 2.0 and Firewire external hard drives run on a fast interface bus (Firewire does have some advantages over USB 2.0, but both are excellent), have a large (8MB) buffer and spin at 7200 RPM, as opposed to 4200 RPM for most laptop HDD. 

Also, note the tip below regarding “Run Virtual PC at Maximum Speed” … this will give a boost to the VPC’s thread priorities at the expense of the host OS applications.  Depending on what you are using the VPC for, this may be exactly what you want. 

From a PPT Deck:

Guidelines:

  • Ideally Virtual PC performance is at:
  • CPU: 96-97% of host
  • Network: 70-90% of host
  • Disk: 40-70% of host
    • However this is only for optimized guest operating systems running typical loads for a single process
    • The Virtual PC team’s aim is always to provide the fastest possible solution while not compromising compatibility
    • While virtual machines are not slow – there is always the potential for an unusual application to cause performance issues

Performance Tuning

  • Guest Performance – Preferences
  • Check “File … Options”
  • Running guest in background: Enable “Run Virtual PC at Maximum Speed”
  • Running a test on multiple guests: Enable “All running virtual machines get equal CPU time”
  • Memory
  • Host should have a minimum of 256MB, 512MB – 1024MB recommended
  • More memory is recommended for running multiple virtual machines simultaneously
  • Each guest should be allocated memory like it would on a physical machine
  • Virtual machines cannot use paged memory on the host system

Additional Disk Optimizations

  • Virtual Hard Disk size
  • Compress them
  • Defrag guest
  • Clear unused sectors ( Cipher, Eraser, etc.)
  • DO NOT attempt this step on a differencing drive – it will expand the disk to maximum size and you cannot compact it.
  • Compact using Virtual Disk Wizard
  • Enable NTFS compression on host operating system
  • Trades off performance for file size
  • Virtual Hard Disk performance
  • Place the .VHD files on separate spindle from host OS
  • If using Undo or Differencing Disks, place them on an additional spindle

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Scott Hanselman (that's me) on .NET Rocks! Live! Friday 9am PST over Windows Media and Skype.

February 19, 2004 Comment on this post [3] Posted in ASP.NET | DevDays | Web Services | XML
Sponsored By

Tomorrow (assuming I get up) I'll be on .NET Rocks Live! with Carl Franklin and Rory Blyth.  It's like Howard Stern, but with code, and it doesn't suck.

We're going to chat about all sorts of fun stuff. ASP.NET, XML, Web Services, Perf Counters, DevDays, etc.

Please do check it out.  If you're on the East Coast, listen to us at 12pm EST as if we were your lunch time radio show (the .NET Gang, those wacky pundits :) ), and if you're on the West Coast listen to us at 9am PST and pretend we're the .NET Morning Zoo!

If you'd like, check out my first .NET Rocks show, it's archived!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.