ASP.NET MVC 4 options separated by straight slots "/" do not pass arguments correctly

I am trying to follow the convention used by many sites that pass arguments with multiple slashes, as opposed to using the GET model.

That is, I am looking to use a URL, for example:

http://www.foo.bar/controller/action?arg1=a&arg2=b&arg3=c 

In this way:

 http://www.foo.bar/controller/action/a/b/c 

I am currently working with this (mainly) 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 something like

 http://www.foo.bar/Sandbox/Index/a 

or

 http://www.foo.bar/Sandbox/Index/a/ 

The controller and action are called accordingly:

 public ActionResult Index(string args) { return View(); } 

but args is null.

However, if I pass something like:

 http://www.foo.bar.com/Sandbox/Index/a/b 

Then args is "a / b" if required.

I searched for SO and the rest of the Internet, but can't find a solution.

Is there something obvious that I'm missing to fix this behavior?

Am I looking for the wrong terminology?

Note. I was able to reproduce this problem with a new ASP.NET application using Windows Authentication. All that has been done:

Any help is greatly appreciated. Thanks! A similar question, but it doesn't help me: URLs with a slash in the parameter?

+5
source share
1 answer

Nothing ... Here's the problem ...

MapRoute first calls the default route. To fix this, I simply changed the default map route using 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 } ); } } 
+3
source

All Articles