Jul 04

The MVC framework – getting started

Posted in ASP.NET MVC      Comments Off on The MVC framework – getting started

Today I’m going to start posting some personal rumblings about the MVC framework. In fact, I haven’t started developing real apps with it, so I’m starting this series to force me look at it since in my current job I’m no longer working with web apps.

One of things you should keep in mind is that the MVC framework relies heavilly on the new routing assembly. If you don’t know what I’m talking about, then probably now is a good time to refresh your memory.

One of the first thing you need to do when building a new ASP.NET MVC application is define the available routes that exist on your application. As always, the global.asax file is a good place for setting up the route table. As you might recall from one of the post on the routing series, defining a route will result in associating an url pattern with a IRouteHandler. That’s all there is to it!

When you’re defining routes for a ASP.NET MVC application, you’ll normally end up using the default MvcRouteHandler introduced by the platform. That means that the following is probably the simplest route you’ll write when using this new platform:

public static void RegisterRoutes(RouteCollection routes) {
            routes.Add( “default”, //route name
                new Route( "", new MvcRouteHandler() ) //pattern and route handler

Ok, you might also define defaults, constraints, data tokens and even apply a name to the route (check the routing series for more info on that). Even though you can be explicit in the creation of a route,you’ll probably will like to know that the MVC framework introduces several extension methods that reduce the ammount of code you need to write when adding routes. You should use these methods when you’re using the MVC default route handler (MvcRouteHandler) or when you want to prevent access to a specific url (ie,when you use the StopRoutingHandler defined on the routing assembly).

In the current release (preview 3 at the time of writing), you’ll find several overloads of a MapRoute and IgnoreRoute methods. The first (MapRoute) tends to reduce the ammout of code you write when you need to create a new route. Obviously, the IgnoreRoute method should be used when you need to prevent access to a specific route.

So, that means that the previous example could look like this:

routes.MapRoute( "default", "");

It looks good to me!

Ok, now that we’ve set up the route handler, we’re ready to start discussing what happens after a request is dispatched to the MvcRouteHandler. We’ll leave the details for the next post. Keep tuned!