用正斜杠"/"分隔的ASP.NET MVC 4参数.没有正确传递参数 [英] ASP.NET MVC 4 Parameters Separated by Forward Slashes "/" not passing args properly
问题描述
我试图遵循许多站点使用的约定,这些站点传递带有多个正斜杠的参数,而不是使用GET模型.
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
以这种方式:
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
或
http://www.foo.bar/Sandbox/Index/a/
Controller和action分别称为:
The Controller and action are appropriately called:
public ActionResult Index(string args)
{
return View();
}
但是args为空.
但是,如果我输入类似内容:
However, if I pass in something like:
http://www.foo.bar.com/Sandbox/Index/a/b
然后根据需要将args设置为"a/b".
Then args is "a/b", as desired.
我一直在搜索SO和网络的其余部分,但似乎找不到解决方案.
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?
注意:我能够使用Windows身份验证使用全新的ASP.NET应用程序来解决此问题.所有这些都完成了:
Note: I was able to repro this issue with a brand new ASP.NET application using Windows Authentication. All that was done:
- 在VS 2015中创建ASP.NET应用程序
- 选择MVC
- 点击更改身份验证
- 选择Windows身份验证
- 将上述映射路线添加到RouteConfig.cs
- 创建SandboxController.cs并将args参数添加到Index
- 创建Index.cshtml视图
- 使用 http://localhost:55383/Sandbox/Index/a 再现问题
- 使用 http://localhost:55383/Sandbox/Index/a/b
- Create ASP.NET application in VS 2015
- Choose MVC
- Click on Change Authentication
- Select Windows Authentication
- Add the above Map Route to RouteConfig.cs
- Create SandboxController.cs and add the args parameter to Index
- Create the Index.cshtml view
- Repro the problem using http://localhost:55383/Sandbox/Index/a
- Repro the expected behavior using http://localhost:55383/Sandbox/Index/a/b
非常感谢您的帮助.谢谢! 类似的问题,但对我没有帮助:在参数中带有斜杠的URL? >
Any help is very appreciated. Thank you! Similar question, but doesn't help me: URLs with slash in parameter?
推荐答案
没关系...这是问题所在...
Never mind... Here is the problem...
首先,MapRoute调用默认路由. 要解决此问题,我只是将默认"地图路线替换为沙箱"路线.
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.
工作解决方案:
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 }
);
}
}
这篇关于用正斜杠"/"分隔的ASP.NET MVC 4参数.没有正确传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!