November 1, 2013

Choose Between ASP.NET MVC vs ASP.NET Web Form

You can probably imagine MVC is going to be an architectural pattern for the foreseeable future, especially on the web. So it is very important to internalize and understand the major difference between ASP.NET MVC and the older ASP.NET Web Forms.


ASP.NET Web Forms


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 is not all roses and buttercups, 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.

Web Forms have many strengths and weaknesses:


Strengths


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

Weaknesses


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

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.

In 2007, 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. 

MVC has many strengths and weaknesses:

Strengths


1.  Provides fine control over rendered HTML
2.  Cleaner generation of HTML 
3.  Clear separation of concerns
4.  Provides application layer unit testing
5.  Can support multiple view engines, such as Brail, NHaml, NVelocity, XSLT, etc.
6.  Easy integration with JavaScript frameworks like jQuery or Yahoo UI frameworks
7.  Ability to map URLs logically and dynamically, depending on your use
8.  Restful interfaces are used by default 
9.  No ViewState and PostBack model
10. Supports all the core ASP.NET features, such as authentication, caching, membership, etc.
11. Size of the pages generated typically much smaller because of the lack of the ViewState

Weaknesses


1. Not event driven by the framework, so it maybe more difficult for ASP.NET Web Form developers to understand
2. Requires the need to understand, at least at the basic level, HTTP, HTML, CSS, and JavaScript
3. Third party library support is not as strong
4. No direct upgrade path from Web Forms
5. No ViewState and PostBack model which makes it more difficult to preserve state

The pros and cons of MVC have to be weighed just as much as Web Forms, and MVC is not always the logical choice. 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.