Reader Level:
ARTICLE

An Introductory look at the ASP.Net 3.5 Model-View-Controller Framework

Posted by Dipal Choksi Articles | ASP.NET MVC May 14, 2008
In this article we will take a brief look at the Model View Controller Framework extension for ASP.Net 3.5.
  • 0
  • 0
  • 9533

In this article we will take a brief look at the Model View Controller Framework extension for ASP.Net 3.5. We will explore the core concepts and key components of the architecture. We will touch on the advantages and limitations of the MVC framework and look at areas where it would be a fitting choice of architecture.

The Model-View-Controller pattern concept:

In the MVC design pattern, the application is split into the model, the view and the controller. The View corresponds to the visual presentation layer, the Model contains the entities/business objects and the Controller determines the application flow/user interaction.

ASP.Net MVC Framework

The ASP.Net MVC Framework is available as an Extension for ASP.Net 3.5.It's expected to be available as a part of a future release for ASP.Net 3.5. Note that MVC is not included in the recently released .Net 3.5 Service Pack Beta.

It offers an alternative to the Web Forms model in ASP.Net. The web forms model is page-centric and the page life-cycle and view-state are the driving criteria for the web forms model.

The MVC framework uses a Front Controller pattern - i.e. the controller determines the routing of URLs. Paths in the application do not necessarily have to correspond in a one-to-one manner to web pages.

URL Mapping Engine

The ASP.Net MVC framework uses URL Routing to map URLs to controllers and actions. URL Routing is performed by parsing the URL based on patterns and variables are passed to the controller as actions.

Default URL pattern is in the form {controller}/{action}/{id}
For example http://www.mySampleKB.net/HR/Benefits/Show/Leavepolicy

Server www.mySampleKB.net
Application HR
Controller Benefits

The URLRoutingModule object routes the URL Request first and then the MVCHandler Http Handler determines the controller to use and the action to be invoked. The MVC framework automatically parses the name of the controller parameter and invokes the controller that is named with the corresponding name and a suffix of Controller. In the above example, the BenefitsController would have been invoked.

So, in the MVC implementation, you do not need to have a physical URL named Leavepolicy.aspx. The URL Routing will allow you to specify the view to be invoked when the above URL is requested.

Compared to URL Re-writing:

URL Re-writing is a different implementation that is available in ASP.Net. It basically allows the user to request a user-friendly URL and re-directs the request to the actual URL in the application. In contrast, URL Routing does not perform a page redirect.

Developer Details

In order to use the MVC Framework, you would choose the corresponding template from the Visual Studio project templates. You also get a choice to select the test framework.

You would specify the routing mappings and also any defaults for the routing, any constraints for the URLs that are to be supported in the application. You can specify URL paths of unknown depth by marking the parameter with *

Controllers

The ASP.NET MVC framework maps URLs to classes that are referred to as controllers. Controller classes process incoming requests, handle user input and interactions, and execute appropriate application logic. A controller class typically calls a separate view component to generate the HTML markup for the request.

A controller class must implement the System.Web.MVC.IController interface. All public methods are exposed as Actions. If you have a method that should not be exposed as an Action, it needs to be marked with the NonActionAttribute attribute.

View

The View component is responsible for the visual presentation layer in the application. By default, the MVC framework allows using the existing ASP.NET page (.aspx), master page (.master), and user control (.ascx) types for this purpose. When a request is made, first the controller class is selected and the action is determined. The action method along with the incoming parameter value is used to execute business layer code and select a view that renders a response to the user by calling the RenderView method Views should not contain any business logic implementation or data retrieval code.

Model

Model objects are the components that encapsulate business logic and typically handle the path between the database and View component. Model classes are typically stored in the Models subdirectory in the application. Model objects are instantiated by the Controller class

Advantages of the MVC Framework
  • Extensible, plug and play architecture - the components of the application are inherently compartmentalized making it easier to replace a component.
  • Enables the application to be "testable" and lends to the Test-Driven-development model.
  • URL mapping framework results in user-friendly URLs without the file-name extension required. Search Engine Optimization.
  • Supports majority of the ASP.Net features and allows using markup in existing ASP.Net pages/user controls/master pages
Limitations
  • Viewstate is not provided by the framework.
  • Learning curve
When would I use it?

Not all the time. Not a direct replacement for web forms
  • If you have no viewstate requirements or very little viewstate.
  • If you need user-friendly URLs
  • If you need to establish Search-engine optimization
  • E.g. Product Catalog, People Search
Links/References
MVC

Summary

In this article we explored the core concepts in the ASP.net MVC Framework. We have seen the various components and architecture involved in MVC applications and areas where the MVC framework would be a natural selection. With a multitude of technologies available, it may not be possible to have a hands-on for every technology being released but it helps to be in the know regarding the various options available so you can make the right choice at the right time. If you feel the MVC framework is an apt choice for your solution, the next step would be to try out sample applications using the MVC framework.

COMMENT USING

Trending up