Advantages of the ASP.NET MVC Approach

As I hinted in earlier articles in this series, to overcome the deficiencies of the ASP.NET Web Forms solution, Microsoft created an alternative to Web Forms—the ASP.NET MVC framework. In the MVC framework, a whole web application is separated into three components: the model, view, and controller. We will take a close look at this approach in this article, the third part of a four-part series.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 10
November 18, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Introducing the ASP.NET MVC framework

Typically, the model component maintains state by persisting data in a database, while the view component is selected by the controller and renders the appropriate UI. By default, the ASP.NET MVC framework utilizes the existing ASP.NET page (.aspx), master page (.master), and user control (.ascx) for rendering to the browser.

The center and most important controller component takes the responsibility of locating the appropriate action method in the controller, obtaining data to use as the action method's arguments, and handling any errors that might occur when the action method runs. Then, the controller renders the requested view. Figure 1 gives a rough perspective of the way ASP.NET MVC works.


Figure 1-the rough flow between the ASP.NET MVC’s three components


In short, among the three components inside the ASP.NET MVC architecture, the flows listed below will be followed:

  • Browser requests /Products/

  • Route is determined

  • Controller is activated

  • Method on Controller is invoked

  • Controller does some stuff

  • Render View, passing in custom ViewData

  • URLs are rendered, pointing to other Controllers

Framework continued

The ASP.NET MVC framework maps URLs to server code differently from a typical ASP.NET web site. Instead of mapping URLs to ASP.NET pages or handlers, the framework maps URLs to controller classes.

Controller classes, then, handle incoming requests, such as user input and interactions, and execute appropriate application and data logic, based on user input. (ASP.NET MVC controllers implement a pattern known as the Front Controller pattern). A controller class typically calls a separate view component that generates HTML output as the response.

Moreover, note that, by default, each set of components is in a separate folder of an MVC Web application project.

Next, let’s start to delve into what advantages the MVC framework can give us. 

Advantages of the ASP.NET MVC framework

The ASP.NET MVC framework is essentially Microsoft's attempt to create an ASP.NET programming environment centered on the MVC pattern.

To some extent, the MVC framework and ASP.NET Web Forms have in common more or less what cars and motorcycles share. That is, they'll both get you to where you want to go, but they'll do so in somewhat different ways.

Next, let’s see the first feature (also the advantage) that the MVC framework gives us.

The First Advantage

(1) No Resting upon ViewState and Postback

The ASP.NET MVC framework does not use the ASP.NET Postback model for interactions with the server. Instead, all end-user interactions are routed to a controller class. This maintains separation between UI logic and business logic and facilitates testability. As a result, the ASP.NET view state and ASP.NET page life-cycle events are not integrated with MVC-based views.

Also, the MVC framework doesn't consider any URL as the endpoint to a physical server file to parse and compile to a class. In ASP.NET Web Forms, you have a 1:1 correspondence between a URL and a resource. The only exception to this rule is when you use completely custom HTTP handlers bound to a particular path.

In the MVC framework, a URL is seen as the means to address a logical server resource, but not necessarily an ASPX file to parse. So the URLs employed by the pages of an MVC framework-based application have a custom format that the application itself mandates. In the end, the MVC framework employs a centralized HTTP handler that recognizes an application-specific syntax for links. In addition, each addressable resource exposes a well-known set of operations and a uniform interface for executing operations.

So, in the MVC world, you do not bother with the ViewState and Postback any more. And also, the client side HTML contents will become clean without “client side ID pollution” troubling you. For this, we are not going to provide related code illustration, so you can dissect and test yourself according to the "MVCeProduct" sample project provided in the fourth part of this tutorial.

The Second Advantage

(2) More Distinct Separations between the M-V-C

Rather than the traditional ASP.NET Web Forms under which the controller and view are within a page (the .aspx corresponds to the View and .aspx.cs to Controller), by introducing a new REST model, each page in ASP.NET MVC is split into two distinct components -- Controller and View -- that operate over the same Model of data. For a clearer understanding, Figure 2 shows the relationships of the Model, View, and Controller in the sample application provided with this article series.

Figure 2 -the relationships of the M-V-C in the sample application


Another important aim of ASP.NET MVC is to ease the TDD. Since in an MVC project, each component keeps a distinct relationship, this, of course, simplifies the TDD in some degree. In fact, the main causes easing TDD are not only limited to this. From the very beginning of designing the underground infrastructure of the MVC architecture, Microsoft employed excellent design patterns, which may be the most important cause to ease TDD—you can unit test each component individually: the View component, the URL Route, Linq to SQL, the Controller action, etc….

However, we have to admit that by keeping the code-behind class as thin as possible, and designing the business layer appropriately, a good developer could also achieve the separation of concerns by using the ASP.NET Web Forms model without adopting MVC and its overhead. By the way, to gain a deeper insight into practical and advanced design patterns using ASP.NET Web Forms, I highly recommend that you download Microsoft .NET Pet Shop 4.0 and study it thoroughly.

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials