Understanding the ASP.NET MVC Framework

Use the ASP.NET MVC Framework for a Cleaner Separation of Concerns in Your Applications

Joydip Kanjilal

October 30, 2009

4 Min Read
ITPro Today logo in a gray background | ITPro Today

The ASP.NET MVC Framework is Microsoft s offering for designing and architecting ASP.NET applications around the MVC design pattern. This article takes a look at the ASP.NET MVC Framework incorporated in ASP.NET 3.5, its REST model, and how it can be used to isolate the Business Logic Layer and the Data Access Layer efficiently. It highlights the features of the framework, its differences with traditional ASP.NET, and how it benefits in code reusability and maintenance.

What Is MVC Design Pattern, and Why Is It Required?

The MVC Design Pattern is basically composed of three major components: namely, Model, View, and Controller. The Model is your domain object that represents the application s data and is responsible for persisting state. The View is a component that renders the data from the model in the user interface. The Controller is responsible for integrating all these components, manipulating the model, responding to the user interface events, and even selecting the appropriate View that should be shown. Scott Guthrie states in his blog: One of the benefits of using a MVC methodology is that it helps enforce a clean separation of concerns between the models, views and controllers within an application. Maintaining a clean separation of concerns makes the testing of applications much easier, since the contract between different application components are more clearly defined and articulated.

You can take a look at my article on the MVC Design Pattern, where I discus how we can implement this pattern sans the ASP.NET MVC Framework: Implementing the MVC Design Pattern in ASP.NET.

 

The MVC Design Pattern provides code re-use, eliminates code duplication, reduces tight coupling, reduces development costs, facilitates code testing, and decouples the business logic and the data access layers of an application. Let s take a quick look at Microsoft s goals in designing such a framework.

Goals of the ASP.NET MVC Framework

Here are some of the goals of the ASP.NET MVC Framework:

  • Support for Test Driven Design model

  • Support for the IOC containers

  • Support for Dependency Injection

  • Clean URLs and navigation support

  • Pluggable, extensible, and maintainable

  • Support for all existing ASP.NET features

No ViewState, No Postbacks Only REST!

The interface-based design of the ASP.NET MVC Framework is truly remarkable. The interfaces IHttpRequest, IHttpContext, and IHttpResponse in this framework facilitate Dependency Injection and, thereby, loose coupling. Unlike traditional ASP.NET, the ASP.NET MVC Framework does not rely on postbacks and viewstate and has full support for the Representational State Transfer (REST) architectural pattern that defines how data exchange takes place in applications that make use of the ASP.NET MVC Framework. Wikipedia states, The ASP.NET MVC Framework couples the models, views and controllers using interface-based contracts, thereby allowing each component to be easily tested independently. The view engine in the MVC framework uses regular .aspx pages to design the layout of the user interface pages onto which the data is composed. However, any interactions are routed to the controllers rather than using the postback mechanism. Views can be mapped to REST-friendly URLs.

Apart from this, the ASP.NET MVC Framework includes an excellent feature in URL Routing that can be used to map incoming URLs in such a way that the appropriate Controller can handle the request for the action. This reminds me of my Struts days! You can even create outgoing URLs that, in turn, can call the respective Controller that is responsible for handling the action. Awesome!

In this regard, Phil Haack says, It uses a REST-like approach to ASP.NET Web development. It implements each request to the Web server as an HTTP call to something that can be logically described as a remote service endpoint . The target URL contains all that is needed to identify the controller that will process the request up to generating the response--whatever response format you need.

Let's Get Started!

To create the simplest ASP.NET MVC Framework application, follow these simple steps.

From the File Menu, create a new Project and select ASP.NET MVC Application when prompted by the dialog window, as shown below:


Figure 1

Then, name and save the project as SampleMVCWebApplication.

The ASP.NET MVC contains the following folders inside the default directory:

  • Controllers

  • Models

  • Views

Understandably, these folders should contain files that correspond to Controllers, Models, or Views that you would like to use in your application. You can see these folders in Solution Explorer, as illustrated here:


Figure 2

When you execute the application, the output is similar to what is shown in the screen capture below:


Figure 3

I ll explore more about this framework in future articles.

Conclusion

The MVC Design Pattern is a proven pattern and one of the most widely used patterns of its kind. The ASP.NET MVC Framework is Microsoft s attempt to design and implement an ASP.NET programming model based on the MVC Design Pattern. This article has looked at the awesome offering from Microsoft and its applicability. Please send me your comments. Happy reading!

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like