I realize the concept of having routing defined and executed in a specific order but I have the following routes defined:
routes.MapRoute(
"Image",
"Image/{action}/{width}/{height}/{file}",
new { controller = "Image", action = "Thumbnail", width = 250, height = 250 }
);
routes.MapRoute(
"GetCampaignsByCampaignType",
"Endorsement/GetCampaignsByCampaignType/{campaignType}",
new { controller = "Endorsement", action = "GetCampaignsByCampaignType" }
);
routes.MapRoute(
"Help",
"Help",
new { controller = "Help", action = "Contact" }
);
routes.MapRoute(
"Campaigns",
"Campaigns",
new { controller = "Endorsement", action = "Campaigns" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Campaign",
"{campaignUrlName}",
new { controller = "Endorsement", action = "GetCampaign" }
);
The question has to do with the bottom 2 routes. I have campaigns like /Summer, /Winter, /Some-other-name where I have hundreds of these and would like not to hard-code them in. I also have other routes that I want to be hit like /Campaign/Account, /Help/Me/Please (where there are more than one segment /1/2 or /1/2/3)
What is the proper order or definition to allow me to have /1 go to the last route defined above and /1/2 or /1/2/3 go the default {controller}/{action}/{id}
Currently, both scenarios go to the default route {controller}/{action}/{id} including /1. If I reverse them, then /1 works but /1/2 or /1/2/3 does not go to the default routing.
Any help is appreciated.
Found a solution via regex:
This way action is required which defaults to two segments /1/2 (perfect) and never catches any single segment urls and the last route gets hit after it passes all others and simply makes sure that it contains proper characters.