5

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?

asked Jun 3, 2016 at 22:34
2
  • 1
    Show all your routes and the order (do you also have the default route?) Commented Jun 3, 2016 at 22:40
  • Yes. I added it. And I feel pretty dumb right now. I realized the Default was getting called first. Commented Jun 3, 2016 at 22:44

1 Answer 1

3

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 }
 );
 }
}
answered Jun 3, 2016 at 22:40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.