ASP.NET MVC Is My Shrieking Electric Guitar

This is your HTML markup:

Clean HTML markup

This is your HTML markup on drugs ASP.NET:

[more]

Traditional ASP.NET markup

Any questions?

ASP.NET offers a lot of horsepower, especially in its most recent incarnation. I give you jQuery, AJAX, Dynamic Data, control adapters, and more.

But traditional ASP.NET web pages are littered with cruft: hidden fields, obscure Javascript, and a ubiquitous <form> element even on pages which don’t look, act, or talk like forms. The ASP.NET WebForms infrastructure extracts a price. That price is the sanctity of your generated markup.

We could almost call clean markup the sacrificial lamb of ASP.NET WebForms development. It’s what you give up in order to achieve the illusion of state in the stateless medium known as the World Wide Web. If you develop in ASP.NET, then you’ve probably spent a lot of time chasing your tail as I have, ferreting out how to clean up ASP.NET-generated markup to make it palatable:

  • Reduce or removing VIEWSTATE and other hidden fields
  • Relocate Javascript functions from the top of the file to the bottom
  • Use control adapters to tweak generated markup of server controls
  • Create customized HtmlTextWriters and hook them in to your page classes
  • Override Page.Render and accomplish all kinds of mischief
  • Apply regular expression replaces to your finished page output

But the problem with these hacks techniques is that they attack the symptoms, rather than the cause.

Dense, encrusted markup is the natural result of trying to graft a desktop application development paradigm (with events and easily recallable state and so forth) onto an inherently stateless, request-response medium: the World Wide Web. This is what ASP.NET tries to do, and that it succeeds at all (and it does succeed) is a testament to the ingenuity of the ASP.NET development team.

Still: the generated markup is ugly and loaded with boilerplate. There can be no arguing with this, although for many line-of-business applications, the cleanliness of your markup may not matter (although in some worst-case scenarios, unbridled viewstate has been known to grow to a few megabytes in size).

But if you’re a stickler, a purist, or if you’re building something that needs to be lean and mean, a typical ASP.NET WebForms application just doesn’t give you the fine-grained control over your own markup that you’ll want—at least, not without a lot of extra work.

Enter ASP.NET MVC

You’ve probably heard about ASP.NET MVC by now (the official beta was released just a few days ago but the previews have been around for months) so I’ll refrain from ranting about the architectural benefits of MVC versus the tightly-coupled ASP.NET “desktop” paradigm of yore and just point out that markup generated by ASP.NET MVC is an order of magnitude cleaner than what you’ll get under classic ASP.NET:

Clean HTML markup

The above is actual markup generated by an ASP.NET MVC website I threw together last weekend. It’s still ASP.NET—ASP.NET is now a superset which includes both the WebForms and the MVC paradigm within it—it’s just not ASP.NET WebForms.

And it’s clean. It’s tight. It can be applied from the ground up, or grafted into an existing ASP.NET WebForms application. It’s works with AJAX. You can still use server controls. You still have a design surface. Master pages. User controls. All that stuff still applies, if you want it. In the words of Scott Hanselman:

I like to think of ASP.NET MVC as the raw, acoustic version of the more heavily produced and multi-layered ASP.NET WebForms we use today.

To me, ASP.NET MVC is one of the best and most forward-looking technologies to come out of Redmond in years. While it’s not an out-and-out replacement for ASP.NET WebForms, If you’re building web applications on the Windows stack, going forward you may need a very good reason to prefer WebForms to MVC.

Comments

  • Deadprogrammer says:

    If MVC was around back in the day, maybe I would not have given up Microsoft development for good. Now that I’ve tasted Linux and Mac, there’s really no going back though.

  • Poker is Rigged | Forums says:

    I’d use anything if it got me the girls. So screw any other reason ASP.NET MVC is for me!

  • jammus says:

    Lovely stuff. Can’t wait to get started.

  • Terry Smith says:

    Thanks for the post!

  • JamesHip says:

    Cool, I may just give MVC a spin, in this case.

  • Dave R says:

    Good writeup on cleaning up ASP.NET.

    An interesting implementation using MVC is Kigg - a Digg clone although what is on Codeplex doesn’t work with the latest releases.
    I have a (somewhat) working version out at http://www.theburnlist.com/

    Keep the good pokerbot stuff coming!

    Dave

  • Coding the Wheel says:

    The Kigg demo is a good one. I looked at this back in Preview…3? 4?

    http://www.codeplex.com/Kigg

    Rob Conery also has an excellent MVC storefront sample:

    http://blog.wekeroad.com/mvc-storefront/mvc-storefront-part-1/

    And for those who are just getting started, the TaskList demo is a good introduction:

    http://www.asp.net/Learn/mvc/tutorial-01-cs.aspx

  • Anonymous says:

    I hate Led Zeppelin.

  • holysmokes99 says:

    I love Led Zepplin.

  • Popo says:

    Should we be scared as PHP Jockeys?

  • Popo says:

    …Some people never wrote a form tag
    while building enterprise level web apps
    with rapid application development mvc frameworks
    like http://www.atk-framework.com/ (secret tip).
    I love Pet Shop Boys…and its not a shame ;-)

  • Anonymous says:

    Great post. A little light on the information but I like that you’ve accentuated what to me is the selling point of ASP.NET MVC, that’s CLEAN MARKUP. I could give a damn about test-driven development. I could give a damn about clean separation of concerns. Just let me output clean markup so my PHP buddies stop throwing stones.

  • Ed K. says:

    PHP produces clean markup? Gasp. What version are you using?

  • Ed K. says:

    Upvoted just for the pic of Jimmy Page

  • Anonymous says:

    @Ed K.: PHP doesn’t produce any markup on its own. Granted, the language itself looks horrible (even though I’d rather that instead of the mixed form
    unat fest). At least it doesn’t require HTML hacks like ASP.NET does out of the box.

    It’s great to see stuff like this around. Thanks for the post!

  • JagBot says:

    Has anyone else gotta a threatening email from Pokerstars about Coding the Wheel?

  • Vince Offer says:

    They threatened to break my knees.

  • Terry Smith says:

    It’s been 20 days since this article was posted. About time for Daniel Radcliffe to start posting here again. He still hasn’t told us if he’s boned the chick who plays Hermione yet.

  • Adam says:

    I really hope this website is not dead, but without a post in the last month I fear the worst. RIP Coding the Wheel?

  • Terry Smith says:

    I’m hoping he’s off on some secret mission…

  • Anonymous says:

    JagBot - PokerStars sent you an email?

    I spent about 2 mos part time building off of the stuff here and I’m at the point where the entire gamestate in pokerstars is captured in a class. So I have all of the communication and game logic in there. But have been stagnating on the poker part of it.

    Which leads me to my point… In lieu of creating an entire bot that plays for you unattended, it may be better to just make an assistance bot. Something that allows you to play more tables. So if you are a guy who can play 4 tables comfortably, you can make a preflop only bot so you can play 12 tables. That way you are always at the computer and you don’t have to worry about CAPTCHA.

  • alex says:

    this post, dont dead.

    go jamesss

  • popo says:

    Lost my last 25 bucks, while playing drunken ;-)

    …but now Im really able to hit the micros.

    Will reaching homless state ;-)…

    …so where are the news about building a pooker bot?

    …of course Im doing some seriouse cooding stuff too. ;-)

    c. u.

  • Terry Smith says:

    Dude! Where are you? It’s been almost two months!

  • Anonymous says:

    2+2 forum members kidnapped him…

  • popo says:

    Would believe he is catched by some poker vendors
    with an offer could not be rejected ;-)…hu hu hu.

  • D says:

    Two full months… I hope you’re doing good and just decided to stop posting for a while for whatever reason. Take care.

  • popo says:

    Or the greatest bluff of James D. whuah ha ha…

  • alex says:

    merry christmas
    feliz navidad

    from peru

    james devlin.

  • Terry Smirth says:

    Geez, you’re in PERU? Well, Merry Christmas anyways!

  • Anonymous says:

    This page will be the last one I’m affraid :
    Well it was good the time this site was alive.

    Thanks for the tips :)

    Regards

  • David Robbins says:

    Quote from Tenascious-D:
    KG: Dude, now that we have the album out … will it make us more popular … with the ladies?
    Jack: Ahh, yah.

  • Stephen Heller says:

    Hi, listen, I’m pretty new on this blogosphere and Internet thing, so I don’t know if there’s a sort of “subscription” method that I can use [xlpharmac][1]y in order to receive notifications of your new entries…? Thing is I enjoy reading your blog a lot and I’d like to be up to date with your posts!

    [1]: http://www.xlpharmacydeals.com

  • ラルフローレン says:

    辞任したのは牧義夫厚生労働副大臣、森裕子文部科学副大臣、黄川田徹総務副大臣、主浜了総務政務官の4人。4日の持ち回り閣議で了承された。小沢元代表は同日夜、東京.赤坂の料理店で牧氏らと会食した。

     辞任を認めた理由について、藤村官房長官は4日の記者会見で「辞意が固かった」と説明した。しかし、民主党内では「続投させれば国会の空転を招くと考えたからだろう」との受け止めが支配的だ。

  • Leave a Reply