I've a page that should accept two urls, one without an action which should have the default value of "Index" and one with an action which should link to the user given action.
The links:
I came up with the following route:
RouteTable.MapRoute("ThirdParty", "{thirdParty}/{controller}/{action}/{type}",
new { action = "Index" },
new { thirdParty = "^[a-zA-Z0-9]*$", type = @"\d+" },
new[] { @namespace });
Unfortunately this only matches the following route:
Now i've given the action a default value of "Index" and I thought it would match the route, but it doesn't. I'm just getting a 404.
Why doesn't it match the route without the "Index" action? I'm using ASP.NET MVC 4.
Creating 2 routes works fine, but I'm pretty sure it should work in a single route.
RouteTable.MapRoute(name, "{thirdParty}/{controller}/{type}",
new { action = "Index"},
new { thirdParty = "^[a-zA-Z0-9]*$", type = @"\d+" },
new[] { @namespace });
RouteTable.MapRoute(name + "(2)", "{thirdParty}/{controller}/{action}/{type}",
new { action = "Index" },
new { thirdParty = "^[a-zA-Z0-9]*$", type = @"\d+"},
new[] { @namespace });
By no means is it considered a best practice to make a single route that can do everything. If creating two routes does what you need it to, then that is the best solution.
RouteTable.MapRoute(name, "{thirdParty}/{controller}/{type}",
new { action = "Index"},
new { thirdParty = "^[a-zA-Z0-9]*$", type = @"\d+" },
new[] { @namespace });
RouteTable.MapRoute(name + "(2)", "{thirdParty}/{controller}/{action}/{type}",
new { action = "Index" },
new { thirdParty = "^[a-zA-Z0-9]*$", type = @"\d+"},
new[] { @namespace });
Besides, it is not possible to put an optional placeholder in the middle of a route pattern - it only works as the right-most position. So what you are asking for is not possible with a single route using MapRoute
because {action}
is followed by another placeholder.