ASP.NET MVC 4参数由正斜杠分隔“/”未正确传递args
我试图遵循许多站点使用的约定,该约定传递带有多个正斜杠的参数,而不是使用GET模型。
也就是说,我希望使用以下URL:
http://www.foo.bar/controller/action?arg1=a&arg2=b&arg3=c
以这种方式:
http://www.foo.bar/controller/action/a/b/c
我目前有(大多数)工作,使用以下内容:
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 } ); }
但是,如果我传递类似的东西
http://www.foo.bar/Sandbox/Index/a
要么
http://www.foo.bar/Sandbox/Index/a/
控制器和操作适当地称为:
public ActionResult Index(string args) { return View(); }
但是args是null。
但是,如果我传递的内容如下:
http://www.foo.bar.com/Sandbox/Index/a/b
然后根据需要,args是“a / b”。
我一直在搜索SO和网络的其余部分,但似乎无法找到解决方案。
我有什么明显的东西可以纠正这种行为吗?
我在寻找错误的术语吗?
注意:我能够使用Windows身份validation使用全新的ASP.NET应用程序重现此问题。 所有这一切:
- 在VS 2015中创建ASP.NET应用程序
- 选择MVC
- 单击更改身份validation
- 选择Windows身份validation
- 将上面的Map Route添加到RouteConfig.cs
- 创建SandboxController.cs并将args参数添加到Index
- 创建Index.cshtml视图
- 使用http:// localhost:55383 / Sandbox / Index / a重新解决此问题
- 使用http:// localhost:55383 / Sandbox / Index / a / b重新编译预期的行为
非常感谢任何帮助。 谢谢! 类似的问题,但没有帮助我: 参数斜杠的URL?
没关系……这是问题……
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 } ); } }