Chris Roberts

ASP.NET MVC. Like ASP.NET, but without the Voodoo!

• Posted in Programming

Much has been written about Microsoft's new Model-View-Controller (MVC) framework for ASP.NET. The initial reaction of many developers - me included - is that Microsoft are driving us back to the bad old days of 'tag soup'; Server side code sits nestled amongst the HTML just like the traditional ASP pages we were all so glad to move away from.

When you delve a little deeper though, and learn more about the MVC pattern it really starts to make sense. Most of the code you'll write will be in the Controller classes, completely separate from your HTML. Yes, there is a little server side code in the HTML (the View), but if done properly this can really be kept to a minimum.

If you're having similar concerns, I'd urge you to consider how it would have been received if it had been released with the first version of .NET, before the event driven page framework was released. I suspect the answer to this question is that it would have been met with almost immediate and universal approval. It is so far removed from the really bad days of traditional ASP that most of us would have been singing its praises from the highest rooftops.

As it is, we've become spoilt by the page framework. We write our HTML in one place and our page classes in another. We write event handlers for pages, buttons and a whole host of other 'server controls' and can often forget the black magic that ASP.NET is doing to glue it all together for us.

The reality is that our control IDs are getting mangled and our pages are getting bloated with 'viewstate'. The price we're paying for the separation of our code from our HTML is a severe loss of control over our end product. I suspect there are programmers out there who don't even know or care what is actually happening when a user clicks a 'link button' on one of their pages. They simply know that when they do, their code runs.

ASP.NET MVC may at first seem like a step backwards but, on reflection, I think the page framework just stepped too far forwards! ASP.NET MVC gives us the ability to control exactly how our HTML gets rendered and how it interacts with the server at a much lower level. It removes a lot of the magic and mystery which surrounds the page framework and allows us to get our hands dirty again.