December 29th, 2008

Introducing the ASP.NET MVC (Part 2) – ASP.NET MVC vs. ASP.NET Web Forms

This is a continuation of my Introduction to ASP.NET MVC series.  As I outlined before this is in an effort to write the book and keep blogging, I decided to write/blog the last chapter, Chapter 2.  I am doing this so I can receive feedback on this chapter as early as possible.  Because this chapter, in my opinion, is probably the most critical of the book, it defines the context around ASP.NET MVC and how it differs from ASP.NET Web Forms, as well as giving a historical perspective of the MVC pattern.

In the next several posts we will cover the following parts of Chapter 2 from the book:

by Nick Berardi

New: $31.49
This item has not yet been released. You may order it now and we will ship it to you when it arrives.

ASP.NET MVC vs. ASP.NET Web Forms

As you have seen, in the previous section, and can probably imagine MVC is going to be an architectural pattern that is going to be around for the foreseeable future, especially on the web.  So it is very important to internalize and understand the major differences between ASP.NET MVC and the older ASP.NET Web Forms.

ASP.NET Web Forms

Starting with the .NET Framework Version 1.0, in January 2002, Web Forms was Microsoft’s first real attempt to provide a first class web application layer that was both robust and flexible enough to meet the demands of the web at that time.  ASP.NET Web Forms has proven to be a mature technology that runs small and large scale websites alike.  Web Forms, was built around the Windows Form construction, where you had a declarative syntax with an event driven model.  This allowed visual designers to take full advantage of the drag and drop, WYSIWYG, interface that they had become accustom to under Windows Forms development in Visual Studio 6.0.  In that they only needed to drop controls onto the ASP.NET page and then wire up the events, as was common in Visual Basic 6.0 development at the time. This made Web Forms a natural choice for Windows Forms developers, because the learning curve was low and the need to understand HTML and many of the web centric technologies almost zero.  Web Forms have many strengths and weaknesses:

Strengths
  • Mature technology
  • Provides very good RAD development capabilities
  • Great WYSIWYG designer support in Visual Studio
  • Easy state management
  • Rich control libraries from Microsoft and third party vendors
  • Abstracts the need to understand HTTP, HTML, CSS, and in some cases JavaScript
  • ViewState and PostBack model
  • A familiar feel to Windows Forms development

Web Forms has grown so much since 2002 because it has the ability to do great things that are often much harder to accomplish in other frameworks.

Weaknesses
  • Display logic coupled with code, through code-behind files
  • Harder to unit test application logic, because of the coupled code-behind files
  • ViewState and PostBack model
  • State management of controls leads to very large and often unnecessary page sizes

Web Forms is not all roses and buttercups, there are some serious setbacks that usually show up when you are trying to optimize your code for scalability, the biggest problems are the ViewState and PostBack model.  ViewState is a way to store the state of the controls, such as data, selections, etc, which is needed to preserve the Windows Form like development habits of the developers.  ViewState was necessary, because the web is a stateless environment meaning that when a request comes in to the server it has no recollection of the previous request.  So in order to give state to a stateless environment you need to communicate the previous state back to the server, in Web Forms this was accomplished using hidden <input /> fields that can become ridiculously large. This increased size becomes apparent when server controls such as GridView are added to the page.  PostBack was another creation to facilitate the Windows Form development feel, it renders JavaScript for every subscribed event, which leaves web developer less control over how the browser communicates with the server.

ASP.NET MVC

ASP.NET was often overlooked as a viable platform for modern highly interactive websites that required a very granular control over the output of the HTML, because of the lack of control over the rendered HTML.  This granularity of control was sacrificed in Web Forms to make if more like Windows Forms development, in other words easier for the drag and drop developers.  This lack of control over the HTML rendering forced developers to move the platforms such as PHP and Ruby on Rails, which offered the level of control they required and the MVC programming model that provided a necessary separation of concerns for their highly complex web applications.

This led Microsoft to announce in the Fall of 2007 that they were going to create a platform based off of the core of ASP.NET that would compete against these other popular MVC web centric platforms.  Microsoft implemented ASP.NET MVC to be a modern web development platform that gives a ‘closer to the metal’ experience to the developers that program with it, by providing full control and testability over the output that is returned to the browser.  This is the main and most important different between Web Forms and MVC, in my opinion.  MVC has many strengths and weaknesses

Strengths
  • Provides fine control over rendered HTML
  • Cleaner generation of HTML (well as clean as you keep it)
  • Clear separation of concerns
  • Provides application layer unit testing
  • Can support multiple view engines, such as Brail, NHaml, NVelocity, XSLT, etc.
  • Easy integration with JavaScript frameworks like jQuery or Yahoo UI frameworks
  • Ability to map URLs logically and dynamically, depending on your use
  • RESTful interfaces are used by default (this helps out with SEO)
  • No ViewState and PostBack model
  • Supports all the core ASP.NET features, such as authentication, caching, membership, etc.
  • Size of the pages generated typically much smaller because of the lack of the ViewState
Weaknesses
  • Not event driven by the framework, so it maybe more difficult for ASP.NET Web Form developers to understand
  • Requires the need to understand, at least at the basic level, HTTP, HTML, CSS, and JavaScript
  • Third party library support is not as strong
  • No direct upgrade path from Web Forms
  • No ViewState and PostBack model (makes it more difficult to preserve state)

As you can see the pros and cons of MVC have to be weighed just as much as Web Forms, and MVC is not always the logical choice.

How do I choose?

It’s up to you to decide and you choice needs to be weighted with a number of other factors, such as team and application requirements, when deciding which ASP.NET technology to implement.  I have developed the following worksheet that will hopefully help you decide, when you need to make this decision.

The Worksheet

This worksheet is meant to be a guide for when you are trying to choose between Web Forms and MVC.  The values in the Value column are representations of how easy it would be to accomplish the requirement in either MVC or Web Forms.

The values range from 0 to 30, zero being no effort to implement and 30 requiring a great amount of effort to implement.  If the value is positive then it is an advantage to MVC, if the value is negative then it is an advantage to Web Forms.  For example if the value is, +4, then the requirement is more suited for MVC, and it would take a little work to get it to work as easily in Web Forms.  Alternatively, if the value is, -25, then the requirement is definitely suited for Web Forms, and it would take a lot of work to get the requirement to work with MVC.

Requirement Value Selection
Clean HTML Rendering

+4

Use RAD Designers

-5

Use TDD (Test Driven Design)

+8

Testability

+7

Data-heavy application

-10

Upgrade from Web Forms

-25

Need event-driven model compared to Windows Forms programming model

-7

Work on an Agile Team

+4

Need separation of concerns

+10

Creating a proof of concept or prototype

-6

SEO

+3

RESTful interface

+3

Need to preserve state of request

-2

Building an Internet Application

+3

Building an Intranet Application

-3

Need to support multiple views of the same application through mobile, web, and REST API

+7

Need to use existing third party controls for ASP.NET Web Forms

-10

Need control over the URL that is generated

+5

So after you make your selection and add up all the values, you should end up with a number representing your project.  Check that number against the table below, and you should have your answer as to which technology, MVC or Web Forms, is best for your project and team.

Value Reasoning

< -50

No Brainer, go with Web Forms

-25

Very Strongly Web Forms

-10

Strongly Web Forms

-3

Slightly Web Forms

0

It is a tossup between Web Forms and MVC

+3

Slightly MVC

+10

Strongly MVC

+25

Very Strongly MVC

> +50

No brainer, go with MVC

I want to mention one last thing if you have want to do new development in MVC, but you have to maintain some legacy Web Form pages, you can mix MVC and Web Forms in the same application. You just need to be aware of the differences between the two, and that you probably will not be able to share any of the Web Form front end code with MVC, such as Themes, Master Pages, and User Controls.

As you probably can guess by the name of this book, The Beer House application landed well in to the MVC side of the worksheet.  Which probably doesn’t surprise you if you are reading this book!

Feel free to use the above decision table and modify it for your own teams’ decision process.  The information provided above however should arm you with most of the important information that you need to know when deciding which way your project should go technology wise.

This post is licensed under a different license than the rest of my site. Copyright © Wiley Publishing Inc 2009

Tags: , , , , ,

Social: kick it on DotNetKicks.com

This entry was posted on Monday, December 29th, 2008 at 6:42 pm and is filed under ASP.NET, C#, Personal. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

13 Responses to “Introducing the ASP.NET MVC (Part 2) – ASP.NET MVC vs. ASP.NET Web Forms”

  1. DotNetKicks.com Says:

    Intro to ASP.NET MVC (Part 2) – ASP.NET MVC vs. ASP.NET Web Forms…

    You’ve been kicked (a good thing) – Trackback from DotNetKicks.com…

  2. LeeDumond.com Says:

    TheBeerHouse MVC Beta Now on CodePlex…

    Tired of bitching about Oxite? Here’s a new MVC sample application to sink your teeth into — an MVC version of the famous TheBeerHouse Starter Kit….

  3. Lee Dumond Says:

    Nick,

    Nice work so far. A couple of things I would point out:

    1. You mention “ViewState and PostBack model” as both a strength and a weakness of WebForms. This could be confusing to a reader. You clearly explain why it is a weakness, but not why it’s a strength.

    2. Right before the “How Do I Choose?” section, it looks like you cut off in mid-sentence there.

    3. Some of your worksheet criteria bear more explanation. Exactly *why* are WebForms more suited to data-heavy applications, for example? Maybe some of these criteria are explained elsewhere in the book, I don’t know.

  4. Danny Says:

    Nick ,

    Very clear and informative writing style. What’s the eta for your book ?
    Will there be an ebook version ?

    thanks

  5. Nick Berardi Says:

    Hi Danny, The ETA for the book is April 2009. I think there will be an ebook version, but it is up to the publisher.

    Lee, Thanks for catching that problem you mentioned in #2, I was actually moving stuff around, for flow, and missed that. Also I plan on addressing #1 and #3 on my second pass, tomorrow when I am fresh.

  6. Reflective Perspective - Chris Alcock » The Morning Brew #253 Says:

    [...] Introducing the ASP.NET MVC (Part 2) – ASP.NET MVC vs. ASP.NET Web Forms – Nick Berardi shares another chunk of his book on ASP.NET MVC, this part looks at the benefits and limitations of ASP.NET MVC and Web Forms and gives a way of deciding which to use for a particular project using a scoring system. Lee Dumond highlights TheBeerHouse MVC Beta Now on CodePlex which is a beta release of the application built for this book. [...]

  7. ASP.NET MVC Archived Blog Posts, Page 1 Says:

    [...] to VoteIntroducing the ASP.NET MVC (Part 2) – ASP.NET MVC vs. ASP.NET Web Forms (12/29/2008)Monday, December 29, 2008 from http://www.coderjournal.comNET Web Form developers to understand … As you [...]

  8. Eric Says:

    I prefer ebooks to “dead tree” books. I’ve already signed up for a couple “preview” ASP.NET MVC books already. I’m not sold on ASP.NET MVC because I feel there is still a ton of mileage that you can get out of ASP.NET if you delve into custom server control creation.

  9. Nick Berardi Says:

    Eric, MVC isn’t suppose to replace Web Forms, after MVC is released there is going to be plenty of growth and work will not stop on ASP.NET Web Forms. That being said Microsoft is just creating an alternate way to develop off of ASP.NET platform. I predict that you will start to see a Internet/Intranet split, being that the majority of Internet developers will choose MVC and a majority Intranet developers will stick with Web Forms.

    Also as a side note when you say ASP.NET the terminology is going to be changing in the future, because both MVC and Web Forms will be running off of the core ASP.NET technology.

  10. Dew Drop - December 30, 2008 | Alvin Ashcraft's Morning Dew Says:

    [...] Introducing the ASP.NET MVC (Part 2) – ASP.NET MVC vs. ASP.NET Web Forms (Nick Berardi) [...]

  11. Code Monkey Labs Says:

    Weekly Web Nuggets #45…

    Pick of the week: Thinking Only of the Junior Developer General Internals Viewer for SQL Server : A neat tool for visualizing the SQL Server storage engine. Introducing the New Task Type : Daniel Moth shows how the new Task type in .NET 4 can make para…

  12. rajuyusuf Says:

    Nice article. I found interesting post about how to handle ASP.NET MVC postback.

  13. James Says:

    One thing I see missing in all these comparisons is the business side – very few of the criteria mentioned actually reflect the business side of the decision making. What is needed is some type of comparison of the time / labor needed to produce equivalent apps in each framework. The business might want to look at some of the other intangibles, but from a business perspective, if the MVC approach drives the cost up 2x, then none of the ‘nice’ features of MVC may matter. Software costs are driven by labor costs – and it’s the single biggest decision point on selecting technology.

Leave a Reply