0

In the past I have done development MediaWiki and am interesting in creating a route similar to the Wiki format {namespace}:{article}.

In the process of testing out my creation but am running into problems with the URL pattern.

routes.MapRoute(
    name: "Generic" ,
    url: "{controller}:{name}" ,
    defaults: new {
        controller = "Article" ,
        action = "View" ,
        name = "Home"
    } ,
    constraints = new {
        name = @"^[\w]+$"
    }
);

Currently the problem is the colon :. In order for the url to work the way I need, I have to have the colon in the url for it to parse it out.

This MapRoute is also the only Route I have so far.

Was wondering how I should create the MapRoute for MVC so that the colon notation is optional, and defaults to the Article Controller.

GoldBishop
  • 2,820
  • 4
  • 47
  • 82

1 Answers1

1

Just put it above the default route.

Routes are navigated in the order they are added. So by putting this route above the default route, it will be tested against the URL first. If it fails, the next route is tested.

// Your route here

// Default route here

If you need it to be optional, then you need to specify two routes. Put them in the order you want them to be checked and make sure your default route stays at the bottom. The default route can then act as a "fallback" for anything that fails.

You can't use a constraint for this.. there is no way for an optional colon character to be resolved. What I mean is.. the routing engine cannot deduce the Controller and "name" from a string such as "HomeGoldBishop". Whereas, it can deduce it from "Home:GoldBishop". You will always need something to fall back on like the default route.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • OP states this is the only route defined in the route dictionary. – Tommy Dec 01 '13 at 23:35
  • I'm not seeing where OP states that adding another route is not an option... ? – Simon Whitehead Dec 01 '13 at 23:36
  • He didn't but I read your answer as, just move the route to the top instead of, add your default route back. I now see where you state he must have two routes. – Tommy Dec 01 '13 at 23:38
  • I have provided guidance on why it won't work with a single route also. Hope that makes more sense. – Simon Whitehead Dec 01 '13 at 23:41
  • @SimonWhitehead did well, I was looking to see if there was an alternative. But seems the pattern I used in URL Rewriting back in IIS6 will be the pattern I still have to use. Thank you much. – GoldBishop Dec 01 '13 at 23:52