asp.net mvc routing pattern is
{"some_parameter/{controller}/{action}/{id}"}
Is this a valid format if some_parameter can be null or string empty
asp.net mvc routing pattern is
{"some_parameter/{controller}/{action}/{id}"}
Is this a valid format if some_parameter can be null or string empty
I believe that what you wanted is {some_parameter}/{controller}/{action}/{id} (notice curly brackets around "some_parameter") and in that case it shouldn't be null or empty, I think. How do you think your end URL might look like to match the route in case when some_parameter is empty? "mysite.com//mycontroller/myaction/myid"?
Routing engine just matches patterns. If you want to handle both {some_parameter}/{controller}/{action}/{id} and {controller}/{action}/{id}, just define both routes.
id as optional) in many cases there might be ambiguous URLs like mysite.com/hello/crazy/world/ which would match both routes and will take the first defined by default. Even ordering is no help here as you might have 2 very similar URLs and want them to match different routes.Edit
I've just reordered the route registration so that it would work:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {
controller = "home",
action = "index",
id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{some_parameter}/{controller}/{action}/{id}", // URL with parameters
new {
some_parameter = UrlParameter.Optional,
controller = "home",
action = "index",
id = UrlParameter.Optional
}
);
They should be registered in that order. Additionally the second route requires an id and some_parameter parameter otherwise it will never be hit because of the route before it. Even though the some_parameter and id parameters are set to optional, that would never happen because the route before would catch it if it was empty.