Despite the fact that the .Net environment has been around for 10 years now, there is still a considerable number of websites that are running classic ASP with the Visual Basic 6 syntax. As a trainer, I still get questions about what technologies to use when my students are facing rewrites of existing sites. In this article, I will explain the general approaches between the two major contenders from Microsoft: ASP.Net and ASP.Net MVC.
ASP.Net is a wonderfully productive toolset. If you need to whip up a front end to an existing database, you can drag, drop, and set properties on a rich set of existing controls in order to get the functionality you need. The controls offer a tremendous amount of functionality, particularly template controls like the GridView control. These controls allow the developer to customize the presentation of data to a significant extent. Would you like one of your columns to present a dropdown list of choices when it is being edited instead of just a regular textbox? Little to no programming required, just configure a template column in the GridView with the appropriate functionality.
In ASP.Net 2.0 (Visual Studio 2005), Microsoft introduced a number of important enhancements to ASP.Net. The most significant of these was the introduction of Master Pages. A Master Page is used to control the layout and basic style of multiple pages in a site. Individual content pages simply plug content into placeholders in the Master Page. A typical Master Page will contain elements for the header and footer, and the content will be plugged via the content page to deliver a complete composite page for use by the user.
Microsoft has implemented its own styling mechanism in ASP.Net, called Themes. Using themes, you can change the style and appearance of the various server controls on a control-by-control basis. You can have a general control for most TextBoxes, for example, and a specific named theme for select TextBoxes that either adds or removes styling elements. Theme settings go beyond the capabilities of traditional CSS; for example, the GridView can have different background colors for alternate rows to give a greenbar paper effect. Themes can also work in conjunction with traditional CSS styling elements, and if there is a conflict between a theme setting and a CSS setting, the developer can choose which overrides the other.
I’ve really just touched the surface of all the rich functionality that ASP.Net provides. The ability to do authentication against either an existing Active Directory domain or a custom database of usernames and passwords, authorization to control which groups of users have access to which parts of the site, profiles to store information for individual users, caching, data binding, and many other features make ASP.Net a very powerful, very productive environment.
Despite all of this functionality, Microsoft has been losing market share on the Internet steadily ever since the introduction of ASP.Net. The question is, why? Sure, Microsoft’s development environment has generally required an up-front investment, but that changed with the free versions of Visual Studio (the Express editions) that provided most of the functionality of ASP.Net for no charge. The other side to that argument, as any good IT manager will tell you, is that it is worth investing in a good toolset if it allows their developers to be more productive.
My opinion is that the answer is composed of two parts: performance and a server-centric focus. Let’s dig a little deeper.
On the performance side, Microsoft made the initial decision when developing ASP.Net to make the development experience as much like programming applications for the Windows operating system (Winforms) as possible. In other words, a button object on an ASP.Net form has a click event, just like a button on a windows form. The code to handle the event is very similar, with the same numbers and types of parameters handed to each event. In order to make the two environments work similarly, Microsoft implemented a hidden control on each page generated by ASP.Net called the ViewState. The ViewState contains a version of the web page, as understood by the ASP.Net engine, before the page is modified by user interaction. The ViewState decreases performance in two significant ways. First, there is more information embedded into the page, making the bandwidth used by ASP.Net pages significantly greater than the same pages written with competing technologies. Second, every time a page submitted by a user for processing, the ViewState information is parsed, and the two states of the page (the “before” view as stored in the ViewState and the “after” view as submitted by the user) are compared to check for any differences. This needs to be done so that the proper events are fired in the proper order, which makes things very nice for the developer. However, all that parsing and comparison takes server CPU time, which impacts performance.
Now, before we criticize Microsoft too harshly for this architecture, let’s try to get a larger perspective. I tell my students that I generally have no problem having the computer work harder to make my life easier. I can sleep well at night with that kind of a tradeoff, because it allows my work hours to be much more productive. Making web programming look and feel like Windows programming is a legitimate approach to development. After all, if this argument were not true, we would still be writing code in assembly language instead of higher level languages like Visual Basic and C#.
Another point: it is certainly possible to scale a website using ASP.Net. SharePoint, for example, is built on the foundation of ASP.Net, and most SharePoint installations that face the internet have between 2 and 10 servers to handle the load. ASP.Net has facilities to allow a website to scale over multiple servers as well, with a bit of care taken by the developer. And if the site is an internally facing site, one server is usually quite sufficient to take care of the job in all but the largest organizations.
So if performance is adequate, that leaves the server-centric focus of ASP.Net. And this, I believe, is the real Achilles heel of this development environment, particularly for the Internet.
ASP.Net MVC is Microsoft’s solution to these frustrations, and it is a very good one indeed. The industry will tell you that MVC stands for Model View Controller, a design pattern for implementing web pages that separates the Model (the data and business rules managed on the server) from the View (the appearance of the page), using the Controller as the orchestra conductor to interact with the data from the Model and use it to generate an appropriate View. Fine. That’s true. But it misses the bigger picture.
The bigger picture is that the MVC design pattern allows the developer to have explicit, fine-grained control over all aspects of the HTML being generated and sent to the browser. Using ASP.Net MVC version 3 and later, there are two possible view engines that can be used. There is the traditional WebForms engine which uses all the Server Controls and the ViewState, as with traditional ASP.Net. There is also a new view engine called Razor, which will feel quite comfortable to a developer migrating from ASP classic (non- .Net). The Razor engine allows Visual Basic or C# to be intermixed with the HTML to control the generation of the HTML.
Also, much of the existing functionality from traditional ASP.Net has been carried over into the ASP.Net MVC environment. The authentication and authorizations subsystems are still available to authenticate against Active Directory accounts or a database of usernames and passwords. Caching is available. The MVC environment implements master page functionality for both view engines. It really seems to be the best of all worlds.
So with all this functionality and customizability, what is the downside? The biggest downside is the learning curve for existing developers. However, with the choice of view engines, and the ability to leverage either Server Controls or using hand generated HTML, the flexibility of the environment is wonderful. It just takes some education to be able to learn the capabilities of the various environments and decide which approach is best for a particular situation.
If you find yourself in a situation trying to decide how to migrate an existing ASP classic (non- .Net) site to newer technology, I would encourage you to take a good, close look at ASP.Net MVC. You can get started by downloading the free Visual Web Developer 2010 Express edition, which includes most of the ASP.Net and ASP.Net MVC functionality for no charge.
To learn more about Microsoft .Net MVC and Web Forms applications, take a look at the MS10264 – Developing Web Applications with Visual Studio 2010 course scheduled at LRS Education Services.