I am trying to follow a convention used by many sites that pass in arguments with multiple forward slashes, as opposed to using the GET model.
That is, I am looking to consume a URL like:
http://www.foo.bar/controller/action?arg1=a&arg2=b&arg3=c
In this fashion:
http://www.foo.bar/controller/action/a/b/c
I currently have this (mostly) working, using the following:
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Sandbox",
url: "Sandbox/{action}/{*args}",
defaults: new { controller = "Sandbox", action = "Index", args = UrlParameter.Optional }
);
}
However, if I pass in something like
http://www.foo.bar/Sandbox/Index/a
or
http://www.foo.bar/Sandbox/Index/a/
The Controller and action are appropriately called:
public ActionResult Index(string args)
{
return View();
}
but args is null.
However, if I pass in something like:
http://www.foo.bar.com/Sandbox/Index/a/b
Then args is "a/b", as desired.
I have been scouring SO and the rest of the web, but can't seem to find the solution.
Is there something obvious I am missing to correct this behavior?
Am I looking for the wrong terminology?
Note: I was able to repro this issue with a brand new ASP.NET application using Windows Authentication. All that was done:
Any help is very appreciated. Thank you! Similar question, but doesn't help me: URLs with slash in parameter?
Never mind... Here is the problem...
The MapRoute is calling the default route, first. To fix it, I just swapped the Default map route with the Sandbox route.
I hope this helps someone.
Working Solution:
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Sandbox",
url: "Sandbox/{action}/{*args}",
defaults: new { controller = "Sandbox", action = "Index", args = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With