Geeks With Blogs
Jakub Malinowski's blog The blog about ASP.NET
Hi!,
Today I'm gonna consider UrlRouting, despite the fact, that actually it is not a part of ASP.NET MVC. Though, we need UrlRouting to understand MVC. Scott Guthrie also promised, that UrlRouting namespace's source code will be realeased with final version of MVC. I'm really waiting for that to happen, because reflector has little problems with extracting source code, and gives me code that cannot be compiled.

[CompilerGenerated]
private RouteBase <Route>k__BackingField;

This kinda code can be found in many places and I'm too lazy to make it working and also I have no task in doing so. Because only thing I can change is maybe a char that divides variables url from "/" to e. g. ",". And for now I have no more ideas, how recompiling of System.Web.UrlRouting could be usefull.

So how we use UrlRouting?
For first UrlRouting is a module, that you have to define in web.config. But don't worry, MVC template does it for you.

<httpModules>

<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

</httpModules>


This module does two things (most important of all) - produces class with information about variables read from Url (domain is cut out) and calls a controller, which you defined to be called. So here is how UrlRouting interpret your Url. The easiest definition is e. g.:

{controller}/{action}/{id}


Every {variable} block is string that contains alphanumeric chars, so "fgerxc", "jakubmal", also "12-12-2008", but not "a/a", as you see in "a/a" second "a" will be treated as next variable. But to do more sophisticated trick you can use {*variable}, and this kinda block takes everything, it doesn't matter what is inside, so string with "/" matches it as well:

{controller}/{action}/{*id}


and url like "http://localhost/Home/About/a/a/a/a" will match it, but won't match previous definition, so {*variable} is always to be placed at the end of your url definition. Now, you can ask: Ok, but what when I want only dates in id? No problem. You use IRouteConstraint and write bool IRouteConstraint.Match, that is easy, huh?

    1 class FooConstraint : IRouteConstraint

    2     {

    3         bool IRouteConstraint.Match(System.Web.HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)

    4         {

    5             return (string)values[parameterName] == "Foo";

    6         }

    7     }


You see that, right? Get the picture? You have access to everything, and you can lock everything simply returning false. But if it Url finally doesn't match any route, you'll get a nasty error.

So it's a time now for whole Route definition:

    1 routes.MapRoute(

    2                 "Default",                                              // Route name

    3                 "{controller}/{action}/{id}",                           // URL with parameters

    4                 new { controller = "Home", action = "Index", id = "" },

    5                 new { id = new FooConstraint() }

    6             );


Third argument is list of default values, and fourth is list of IRouteConstraints, which I showed you before. Analogically you have IgnoreRoute() with only two parameter - url to ignore and eventually list of IRouteConstraints, defined same as url for MapRoute(). But IgnoreRoute and MapRoute are extensions that come with MVC and UrlRouting can work without MVC, so

    1             routes.Add(new Route(

    2                 "{controller}/{action}/{id}",

    3                 new StopRoutingHandler()

    4                 ));


Second parameter of Route contructor is IRouteHandler, which is gonna handle current request, StopRoutingHandler returns request to normal ASP.NET handler, which loads a page for given address. IgnoreRoute and MapRoute are actually... oops sorry I cannot copy them here, because they would make a real junk here, so I'll only tell you. MapRoute is extension, that has Route contructor inside, and MVC handler (I'll write you more about it in next posts) as handler to be called for request and IgnoreRoute is the same, but has StopRoutingHandler as handler.

Here I gave you basic information about UrlRouting and I think that it is clear for you now. If you like or dislike something, please write in comments. Also tell me if you wish next post to be about creating custom IRouteHandler.

See you next post!
Posted on Thursday, December 4, 2008 9:40 PM ASP.NET MVC | Back to top


Comments on this post: ASP.NET MVC - UrlRouting (overview) - part1/1

Comments are closed.
Comments have been closed on this topic.
Copyright © jakubmal | Powered by: GeeksWithBlogs.net