Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 Parameters Separated by Forward Slashes "/" not passing args properly

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:

  1. Create ASP.NET application in VS 2015
  2. Choose MVC
  3. Click on Change Authentication
  4. Select Windows Authentication
  5. Add the above Map Route to RouteConfig.cs
  6. Create SandboxController.cs and add the args parameter to Index
  7. Create the Index.cshtml view
  8. Repro the problem using http://localhost:55383/Sandbox/Index/a
  9. Repro the expected behavior using http://localhost:55383/Sandbox/Index/a/b

Any help is very appreciated. Thank you! Similar question, but doesn't help me: URLs with slash in parameter?

like image 833
Thumper Avatar asked Oct 18 '22 08:10

Thumper


1 Answers

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 }
        );


    }
}
like image 184
Thumper Avatar answered Oct 20 '22 22:10

Thumper