views:

40

answers:

1

I'm looking to write a custom route that would allow the following

http://localhost/blog/tags/foo

Currently this is what actually works

http://localhost/tags/Index/nhibernate

I've tried the following with no success - any help would be appreciated

routes.MapRoute( "Tags", "{controller}/{id}", new { Controller = "Tags", action = "Index", id = UrlParameter.Optional } );

routes.MapRoute( "Tags", "blog/{controller}/{id}", new { Controller = "Tags", action = "Index", id = "" } );

+2  A: 

You could use in your global.asax something like this:

routes.MapRoute("Tags",
                "blog/tags/{TagName}", 
                new { Controller = "Tags", action = "ShowTag", TagName = "" });

You'd then need a controller called 'TagsController.cs' with an ActionResult method called ShowTag plus a corresponding aspx called ShowTag.aspx. Your ShowTag method should look something like this:

public ActionResult ShowTag(string TagName)
{
    //do stuff here to get Id from tag name and get other data etc...
    return View();
}

Note that the order in which you map routes in the Global.asax.cs does matter.

mdresser
dude, you fn rock! Turns out the ORDER was the issue. I put this Tags route first and all was good! Thanks a ton!
Toran Billups
No problem, the order of routes has had me stumped a few times before!
mdresser