BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Web Frameworks, MVC, and ASP.NET

Web Frameworks, MVC, and ASP.NET

This item in japanese

After nearly a year as a community tech preview, Microsoft has released the first true beta of the ASP.NET MVC framework. ASP.NET MVC is a radical departure from the WebForms technology has promoted in the past, and in the opinion of many, a return to mainstream web programming. The MVC pattern provides the cornerstone for web frameworks such as Ruby on Rails and Java's Spring Framework.

MVC web frameworks should not be confused with their namesake, the Model-View-Controller pattern first proposed by Trygve Reenskaug. Under Reenskaug's pattern, the view and controller were tightly bound to one another with a one-to-one mapping between each view and controller. With a MVC web framework, the view and controller are loosely coupled and it is not unheard of for multiple views to be tied to a single controller.

Regardless of which definition of MVC you prefer, the model remains an independent representation of data that has no knowledge of what is using it. This is in contrast to WebForms, where it is common for data to be stored in the UI elements themselves in the form of viewstate.

Microsoft's MVC framework trades in the quick development time of forms and controls for the flexibility and precision you get from controlling all the HTML output directly. This change in philosophy will probably be a much easier transition for those familiar to classic ASP programmers or non-Microsoft languages than those already with a background in .NET programming.

Some of the new features in this, the first ASP.NET Beta release, seek to guide developers towards this new way of thinking. For example, developers can now create new views simply by right-clicking on the associated controller class or pressing Ctrl-M Ctrl-V, at which time they are prompted for the model the view will bind to.

Another departure from WebForms is the emphasis on JavaScript. While WebForms tried to hide JavaScript from the developer, either by wrapping it in controls or by processing data on the server, ASP.NET MVC embraces it. By default MVC web sites get a "Scripts" folder already pre-populated with ASP.NET AJAX and jQuery. Full intellisense is provided for the former, while jQuery only has partial support. This is temporary however, and the annotations needed for full jQuery support is expected in a few weeks.

Microsoft has always had a fascination with data binding, and ASP.NET MVC is no exception. Microsoft's "Model Binders" allow developers to quickly map HTTP POST data to object properties. These objects are then sent to the controller class's action method. With this release, default binders for common .NET classes were added. Keep in mind, however, that under most circumstances developers will be creating their own.

Automated testing of web sites is another mainstream concept Microsoft is only just now embracing. Unlike many frameworks where testing is only given lip service, Microsoft has planned for it from the beginning. Mocks are no longer needed for testing controllers and models, which is important as they tend to contain all of the testable logic. Testing of views will still have to be done externally as that involves checking the HTML against each supported browser separately.

Another aspect of ASP.NET is a return to a focus on HTTP verbs. While important in older technologies like classic ASP, a WebForms developer could almost forget they existed. Other than knowing that a post-back caused a POST and a Response.Redirect caused a GET, little could be done with them. In ASP.NET MVC, HTTP verbs are very important and the API reflects that. Common tasks like restricting some actions to a particular verb can be done simply by decorating the controller method with an AcceptVerbs attribute.

To facilitate replacing Microsoft's own methods for those written by the developer, any of the HTML helper methods have become extension methods. This allows them to be replaced partially or in whole simply by changing the using/imports statement.

On a concluding note for those die-hard WebForms fans, Microsoft isn't abandoning you. Scott Guthrie writes,

As I always like to make sure I point out: If you don't like the MVC model or don't find it natural to your style of development, you definitely don't have to use it. It is a totally optional offering - and does not replace the existing WebForms model. Both WebForms and MVC will be fully supported and enhanced going forward (ASP.NET WebForms in .NET 4.0 will add richer URL routing features, better HTML css markup support, complete control over the ClientId property, more AJAX features, and more that I'll be blogging about soon). So if you don't like the MVC option, don't worry, and don't feel like you should or need to use it (you don't).

Rate this Article

Adoption
Style

BT