last day (14 days later) » 

12:39 PM
2
A: ASP.NET MVC routing URLs starting with numbers

RobbanYou will have to use custom routing, in your RegisterRoutes method you could add another route that looks something like this: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "ApplesRo...

 
Ah! What if I have dozens or hundreds of pages starting with numbers? Is it possible to create a generic rule that will handle all URLs starting with numbers?
 
Yes, you could do that with a regex route I believe, I'll add an example.
Updated my answer with a more generic example. If you would need to expand it to handle more routes you could probably just build on that example with more route constraints etc.
 
Thanks for that. Looks promising but I don't think the regex rule will handle number URLs with any text after the number. URLs to consider: 40apples, 1orange, 300cherries
 
Updated with another answer that will handle your cases
You could also change [A-Za-z] to a group of names that you would want to match, like \d+(apples|oranges|cherries) That would make it possible to exclude paths like /123bread for example, but would force you to specify all fruits in the constraint.
 
I really like the look of it, I don;t understand it but I like it : ) So in your last example how do I need to name my controllers? I really need to get my head around regex.
 
12:40 PM
Hi, in my last example I automatically route to a Numbers controller.
That means it would be the same controller handling all of the routes
 
I see. Hmm, in my case I need to keep the controllers seperate.
 
routes.MapRoute(
"NumbersRoute", // Route name
"{numberfruit}/{action}", // URL with parameters
new { controller = "Numbers", action = "Index" } // Parameter defaults
,new { numberfruit = @"\d+[A-Za-z]" } //constraint
);
This says that we want to route any routes that adheres to our regex to the Controller NumbersController and the action Index if no action is specifiec
 
Is it possible to take any URLs that start iwth a number and simply append an underscore to the beginning of it so it matches the controller?
 
so any route that starts with 1 or more digits, followed by any character between A-z etc.
absolutely
you could have a route that looks like this
routes.MapRoute(
"NumbersRoute", // Route name
"{number}-{controller}/{action}", // URL with parameters
new { action = "Index" } // Parameter defaults
,new { number = @"\d+" } //constraint
);
this would match any route that starts with a digit, followed by hyphen and then a controller name
so lets say the route /45-cherries/Buy
That would be routed to the Buy action of the Cherries controller
 
I'm really sorry I don't think I explained clearly. I meant if I have 40Apples or 300cherries how can I route to the respective controllers _40apples and _300cherries?
I really appreciate your time btw.
 
12:47 PM
Ah, now that's a different beast...
Then you could try this route
hmm, couldn't find a clean way
 
ah, that's a shame.
 
Actually, what you should probably do for those controllers is use attribute routing
If you're using asp.net mvc 5 that is?
 
I'm not sure what version of mvc is in .NET 4.6
 
You could for example for the _40apples controller have this route prefix: [RoutePrefix(“40apples”)]
 
Ah and will this run before the RouteConfig?
 
12:55 PM
And on the Buy action add a [Route(“{Buy}”)]
Yes, first attributes then the standard routes kick in I believe
Actually, that is up to you I realize
you would put routes.MapMvcAttributeRoutes();
in your routesConfig at the appropriate place
and it would run where you put it, before or after other routes
 
I'm testing now : )
 
Allright, best of luck. I gotta run.
 
ahhj it worked!!
awesome, thank a million. I'll accept your answer and feel free to update with this last bit when you have time.
Thanks again.
 
 
1 hour later…
2:05 PM
No problem, updated the answer with the AttributeRouting as well.
 

last day (14 days later) »