Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

ASP.NET MVC Framework and Routing



The ASP.NET MVC framework is an emerging approach to building ASP.NET applications. Clearly inspired by Castle MonoRail, the ASP.NET MVC framework is based on an extremely popular Web adaptation of the Model-View-Controller pattern -- the Model2 pattern. Microsoft likes to contrast the ASP.NET MVC framework to Web forms by calling them cars and motorcycles. It's a nice analogy. Both cars and motorcycles can take you somewhere else, but with different speed and comfort. Classic ASP.NET is the more comfortable car; the MVC framework is the motorcycle, which is cheaper and hardly gets stuck in traffic jam.

Classic ASP.NET is based on the Page Controller pattern -- an object receives the HTTP request for a given page and handles it. When done, the page controller does something that updates the view. As mentioned, the ASP.NET MVC framework is based on a variation of MVC and takes a more direct route to executing operations. Let's see.

On top of the MVC framework there's a sort of front controller -- the ASP.NET routing subsystem. This component intercepts any HTTP request directed at the site. It parses the URL trying to match it to a list of patterns the developer provided. The typical URL pattern is as follows:


http://server/.../{controller}/{action}/{id}. 

controller, action, and id indicate tokens in the URL that do not point to a physical resource on the server, but rather communicate to the routing system what to do. In global.asax you have the following code to register supported URL patterns:


protected void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute("Default", 
        "{controller}/{action}/{id}", 
        new { controller = "Home", action = "Index", id = "" });
}

Each map route has a name, a pattern, and some default values. The routing system contains some (replaceable) logic to map the request to a route handler. The route handler is ultimately responsible for the action. The default route handler is invisible to developers and the first thing it does is extracting the {controller} and {action} tokens from the URL. If {controller} matches a class in the Web application, the handler instantiates the controller and invokes on it a method that corresponds to the {action} token.

As a developer, you organize your site as a collection of controller classes and related views. All the possible actions that the user of a Web site can accomplish are mapped as action on controller classes. Each action is mapped to a route. The routing system and its route handler companion class coordinate operations.

What's a controller? Quite simply, it is a public class that inherits from the base Controller class and follows a naming convention -- the string in the {controller} token trailed by Controller. Here's a quick example:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Title"] = "Home Page";
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        return View();
    }
}

The controller indicates the action to execute. What about the user interface? The controller is also responsible for instructing the view object to produce HTML. The view object produces HTML based on an ASPX page living under the Views folder. The ASPX page has a code-behind class but usually does all of its work through markup and ASP-style code blocks. In ASP.NET MVC framework the view is an extremely thin object:


<h2><%= Html.Encode(ViewData["Message"]) %></h2>

Communication between controller and view occurs through a container -- the ViewData object.

With the ASP.NET MVC framework, applications go through a simpler runtime environment, but any action has to be expressed in terms of neat action. Not ideal for any website, but just great where it works.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.