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.
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.
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.