.NET Web Forms vs. MVC: Which is Better?

Share on FacebookTweet about this on TwitterShare on LinkedIn

Share on FacebookTweet about this on TwitterShare on LinkedIn

A lot of fuss is being made lately about whether Microsoft’s relatively new Model View Controller (MVC) technology should automatically supplant .NET Web Forms as the superior web application development technology. There is no clear-cut answer, and rightly so; they both provide different means to reach the same end. Even Scott Guthrie of the MVC development team stated that, “Different customers look for different programming approaches, and a lot love Web Forms and think it is great. Others love MVC and think it is great. That is why we are investing in both”. Below, I have tried to highlight some positives and negatives of both frameworks. This is not an attempt to sway you one way or the other, but rather to possibly provide a broader understanding of why the two frameworks exist as viable options for your company’s development needs.

segue-net-web-forms-vs-mvc

What Are .NET Web Forms?

The idea of .NET Web Forms was born from an attempt to address the ever looming issue implicit in the development of classic ASP web sites. Classic ASP pages housed both the web code and the accompanying code-behind logic in one location: the ASP page. In that way, pages could very easily become long spaghetti-code filled documents that often would become unreadable and unmaintainable. For example, the code to handle page submissions was contained in the same page that would subsequently either redirect the user or need to remain on the current page, but would simply show a different section of the page. This type of event handling was unwieldy.

To address this concern, Microsoft attempted to bring their successful, more event-driven, “visual” windows-application development methodology to web development. As a result, they created a framework that worked more like the popular Visual Basic or Visual C++ desktop / forms development. Suddenly, developers could decouple view code from logic code by having most of their web controls on one page and then have all of the code-behind, “smarts” of the page, contained in a completely separate page.

Advantages of .NET Web Forms

  • RAD – Rapid Application Development

    • The mechanisms behind how controls and pages work within an event-driven structure are abstracted out so that the developer generally does not need to know the inner workings of the process
    • Drag & Drop controls provide most of the functionality right out of the box. 3rd party solutions are readily available, and control like Grids generate the HTML and JavaScript for the developer
    • Applications, complete with validation, can be quickly developed by simply setting properties on controls
    • Easy for newbies to “pick up and run” with web application development
  • Cost

    • Being the older, more legacy framework, most .NET web developers have experience with Web Forms, or more specifically the desktop application event driven framework from which it was derived – there is a smaller learning curve implicit in learning the Web Forms framework
  • More Mature

    • Like the old joke goes, “no matter how old you are I’ll always be younger”. Even though MVC is a younger .NET technology (the paradigm methodology, though, has been around for a while), Web Forms was here first and has a larger and more robust legacy implementation

Disadvantages of .NET Web Forms

  • Very little control over HTML produced on the page

    • The ability to simply drag & drop controls enables rapid application development (RAD), but the drawback is that the developer has very little control over what HTML is actually produced on the page
    • Initially, a pretty big issue with Web Forms was not being able to control the HTML produced on the page, meaning that control ID generation was sometimes after-the fact guess work
      • ID management becomes compromised and using client side frameworks like jQuery become more difficult in that control ID’s, or even control nesting / hierarchies, are not always consistent
      • Even though Microsoft has taken steps to improve this in the latest release of MVC, not enough time has passed to determine if they’ve truly resolved this issue
  • Web Forms Life Cycle

    • How many developers truly know the full extent of the Web Forms lifecycle? A lot of developers must become acquainted with the more esoteric lifecycle events as a result of needing to “hook” into 3rd party controls

What is MVC?

The MVC design pattern was not invented by Microsoft. Rather, it has been around for upwards of 30 years, having been first introduced around 1979. Since then, the methodology has been implemented by such well known platforms as Ruby on Rails, Apple’s Cocoa and Apache Struts. Only relatively recently has Microsoft added the MVC design, building atop the .NET framework. As a result, .NET developers have the option to develop applications within the existing .NET world they already know but can eschew the lack of overall control offered by the drag & drop of Web Forms. With MVC they can embrace the same separation of concern (SOC) paradigm that has already proven to be successful in other frameworks. Admittedly, not all MVC implementations are exactly the same, with slight differences existing between the different frameworks. For example, Microsoft’s implementation of MVC more closely resembles Rails and Django rather than either Apple’s Cocoa or even the original implementation of MVC, Smalltalk-80.

Advantages of MVC

  • Multiple Forms

    • Web forms do not allow for multiple forms on a page, whereas MVC allows for any number of forms on the page
  • Similar to other established, non-Microsoft Frameworks

    • Developers with backgrounds in PHP, Ruby, or Python, will find architectural and event methodologies similarities to those implemented in MVC
  • Adheres to the “stateless” nature of the web

    • One big advantage is that Session[] usage is minimized so that the URL’s now become the drivers behind data maintenance. For example, because an MVC application will pass data parameters via the URL or the View binding, the potential “crutch” of using and maintaining Session[] variables is virtually eliminated.
  • Test Driven Development (TDD)

    • TDD is facilitated much more easily
  • Separation of Concern (SOC)

    • SOC is encouraged and easier to implement

Disadvantages of MVC

  • HTML & CSS skills must be resurrected

    • Oddly, one disadvantage is that the developer needs to be fairly competent in HTML and CSS, including dynamically modifying the DOM, styles and client-side events
      • The Views are HTML structures that are written from scratch by the developer
      • Web Forms and their drag & drop controls abstract out this process to the point of automatically creating nearly all the necessary HTML needed to display what is needed, including the accompanying client-side functionality
      • MVC requires the developer to understand how the client-side code and HTML all play together, thereby ensuring consistent, controlled code
  • MVC is still “relatively” new

    • There is the potential added cost of either having in-house developers learn the new MVC framework or needing to pay for outside vendors to provide the work

No one solution is an absolute better choice than the other. The application framework should be chosen based upon the needs and resources of the company doing the development. If you have a development cycle that requires faster than expected turnaround, then Web Forms might be the best option for your company. If your development project has the time, money, and energy to develop an application from the ground up then MVC could potentially be the better option. Again, neither framework is the definite best option, but one could potentially be a better for your development team structure and resources. Segue has extensive experience with both frameworks and would be more than happy to help consult on your development decisions.