我试图遵循许多网站使用的约定,这些网站使用多个正斜杠传递参数,而不是使用 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
or
http://www.foo.bar/Sandbox/Index/a/
控制器和操作被适当地称为:
public ActionResult Index(string args)
{
return View();
}
但 args 为空。
但是,如果我传递类似以下内容:
http://www.foo.bar.com/Sandbox/Index/a/b
然后 args 是“a/b”,根据需要。
我一直在搜索 SO 和网络的其余部分,但似乎找不到解决方案。
我是否缺少一些明显的东西来纠正这种行为?
我是否在寻找错误的术语?
注意:我能够通过使用 Windows 身份验证的全新 ASP.NET 应用程序重现此问题。一切都完成了:
- 在 VS 2015 中创建 ASP.NET 应用程序
- 选择MVC
- 单击更改身份验证
- 选择Windows身份验证
- 将上面的Map Route添加到RouteConfig.cs中
- 创建SandboxController.cs并将args参数添加到Index中
- 创建 Index.cshtml 视图
- 使用重现问题http://localhost:55383/Sandbox/Index/a http://localhost:55383/Sandbox/Index/a
- 使用重现预期行为http://localhost:55383/Sandbox/Index/a/b http://localhost:55383/Sandbox/Index/a/b
非常感谢任何帮助。谢谢你!
类似的问题,但对我没有帮助:参数中带有斜杠的 URL? https://stackoverflow.com/questions/6328713/urls-with-slash-in-parameter