ASP.NET MVC 4参数由正斜杠分隔"/"未正确传递args

Thu*_*per 5 c# asp.net asp.net-mvc asp.net-mvc-4

我试图遵循许多站点使用的约定,该约定使用多个正斜杠传递参数,而不是使用GET模型.

也就是说,我希望使用以下URL:

http://www.foo.bar/controller/action?arg1=a&arg2=b&arg3=c
Run Code Online (Sandbox Code Playgroud)

以这种方式:

http://www.foo.bar/controller/action/a/b/c
Run Code Online (Sandbox Code Playgroud)

我目前有(大多数)工作,使用以下内容:

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 }

        );
    }
Run Code Online (Sandbox Code Playgroud)

但是,如果我传递类似的东西

http://www.foo.bar/Sandbox/Index/a 
Run Code Online (Sandbox Code Playgroud)

要么

http://www.foo.bar/Sandbox/Index/a/
Run Code Online (Sandbox Code Playgroud)

控制器和操作适当地称为:

public ActionResult Index(string args)
{
    return View();
}
Run Code Online (Sandbox Code Playgroud)

但是args是null.

但是,如果我传递的内容如下:

http://www.foo.bar.com/Sandbox/Index/a/b
Run Code Online (Sandbox Code Playgroud)

然后根据需要,args是"a/b".

我一直在搜索SO和网络的其余部分,但似乎无法找到解决方案.

是否有一些明显的东西我缺少纠正这种行为?

我在寻找错误的术语吗?

注意:我能够使用Windows身份验证使用全新的ASP.NET应用程序重现此问题.所有这一切:

  1. 在VS 2015中创建ASP.NET应用程序
  2. 选择MVC
  3. 单击更改身份验证
  4. 选择Windows身份验证
  5. 将上面的Map Route添加到RouteConfig.cs
  6. 创建SandboxController.cs并将args参数添加到Index
  7. 创建Index.cshtml视图
  8. 使用http:// localhost:55383/Sandbox/Index/a重新解决问题
  9. 使用http:// localhost:55383/Sandbox/Index/a/b重新编译预期的行为

非常感谢任何帮助.谢谢!类似的问题,但没有帮助我:参数斜杠的URL?

Thu*_*per 3

没关系...这是问题...

MapRoute 首先调用默认路由。为了解决这个问题,我只是将默认地图路线替换为沙盒路线。

我希望这可以帮助别人。

工作解决方案:

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


    }
}
Run Code Online (Sandbox Code Playgroud)