This is a stripped down example of a problem I was having this morning with ASP.NET MVC's URL routing.
Fairly simple, I wanted a route's Action to be called, whether or not the parameter on the end was supplied.
This route works fine, matching both /apple/ and /apple/test/
routes.MapRoute( "Working Route", "apple/{parameter}", new { controller = "Apple", action = "Action", parameter = UrlParameter.Optional }, new { parameter = @"([a-z0-9\.-]+)" } );
However, this second route will only match /banana/test/ and the like. When I try /banana/, the router just passes right over it and returns the catch-all 404 error.
routes.MapRoute( "Non-Working Route", "banana/{parameter}", new { controller = "Banana", action = "Action", parameter = UrlParameter.Optional }, new { parameter = @"([a-z0-9]+)" } );
The only difference is the Regex validation of the parameter, but as it's quite a simple Regex match, they should both work perfectly fine for a URL like /banana/, yet the second route just fails to recognise it.
I side-stepped my problem by just changing the Regex on route #2 to match that on route #1, and accept the '.' and '-' characters, I just wondered if anyone knows why this seems to be happening.
EDIT:
Here are the Controllers and Actions that I'm using for my example. Nothing fancy here.
public class AppleController : Controller { public ActionResult Action(string parameter) { if (parameter == null) { parameter = "No parameter specified."; } ViewData["parameter"] = parameter; return View(); } } public class BananaController : Controller { public ActionResult Action(string parameter) { if (parameter == null) { parameter = "No parameter specified."; } ViewData["parameter"] = parameter; return View(); } }
So my problem is that /apple/ would display "No parameter specified.", while /banana/ gives me an undesired 404 instead.
So far..
Using parameter = URLParameter.Optional in the Route declaration: Route #1 works perfectly, Route #2 doesn't match without the parameter.
Using parameter = "" in the Route declaration: Both Route #1 & Route #2 fail to match when the parameter is left off the URL.
Declaring parameter = "" in the Action method signature: Not possible due to .NET version.
Removing all other routes has no effect.
Global.asax.cs
file. – Ben Jenkinson