BT

Web Frameworks, MVC, and ASP.NET

| by Jonathan Allen Follow 576 Followers on Oct 20, 2008. Estimated reading time: 4 minutes |

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

What MVC wants to cover? by Willian Leite

I really think about what is the strategy beyond this "new" web way from Microsoft to programming websites.

Is Microsoft MVC the answer for UI tester? Is about more layers for scalability?

I'm really worry about why Microsoft bring this now. In the beginning everybody tells that VB.net and C# are the same, but in a real world programming I saw a lot more space for professional programming for C#. I'm thinking the samething right now.

Willlian Leite
wleite.blogspot.com

Re: What MVC wants to cover? by Francois Ward

What they want to cover is making it easier to get devs in the bandwagon. WebForms may be advertised as the "RAD", easy framework, drag & drop click click, and it was that way in 1.1, but as of 2.0 and beyond, things changed.

WebForms are very powerful and flexible, and allows you to make great composite apps (like Sharepoint), and have an horizontal architecture (as opposed to the more vertical MVC and other similar framework), and totally abstract the context from the components.

That comes at a price though, where even the more senior asp.net devs are not knowledgeable of how it all works or how to get the most out of it. So comes MVC, where the barrier for entry to make a well architecure app is much lower. Sure, its longer to make a quick and dirty prototype, but making a full app will be much, much simpler. You'll lose out on abstraction and the component model, but thats better for many than making a mess with WebForms because the architect of the team doesn't understand it.

I guess it IS a bit similar to VB.NET and C#. They are almost the same, but since VB.NET is advertised as being the "easy, newbie friendly" language, you see many more "disasters" in VB.NET than you do in C#.

Views and Controllers by Chris Sutton

I've been reading the original MVC documents from Trygve Reenskaug and they don't seem to say that there is a one to one mapping between the view and controller. There are several places where it uses the plural "views" relative to a controller.

Chris Sutton

Re: Views and Controllers by Jonathan Allen


Unlike the model, which may be loosely connected to multiple MVC triads, Each view is associated with a unique controller and vice versa. Instance variables in each maintain this tight coupling. A view's instance variable controller points at its controller, and a controller's instance variable view points at its associated view.


st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

4 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT