asp.net-mvc MVC3 Razor URL.Action 参数值,带正斜杠。给出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9621067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:47:43  来源:igfitidea点击:

MVC3 Razor URL.Action Parameter Value with forward slash. giving error

asp.net-mvcasp.net-mvc-3razorcontroller

提问by Arvind

Its a small question on MVC3 Razor, I have a string ID like "a/b" when I am trying to call details or delete method of Controller. Getting error as system assuming "a/b" as 2 parameters but I have to pass this in one string value parameters.

这是关于 MVC3 Razor 的一个小问题,当我尝试调用 Controller 的详细信息或删除方法时,我有一个像“a/b”这样的字符串 ID。当系统假设“a/b”为 2 个参数时出现错误,但我必须在一个字符串值参数中传递它。

--Edit

- 编辑

< a href="@Url.Action("Details", "Search", new {id = "a/b"})">Details </a>

My Controller/method is like Search/Details (string id)

我的控制器/方法就像搜索/详细信息(字符串 ID)

And I want to send id = 'a/b' . but .Net assuming it as 2 parameters in URL.

我想发送 id = 'a/b' 。但是 .Net 假设它是 URL 中的 2 个参数。

Please suggest.

请建议。

采纳答案by Justin Pihony

It appears that forward slashes are not automatically encoded, and the reason is probably because even if they are encoded (%2f), by the time they reach the routing engine they have been decoded back to a forward slash. (Search for Robj in this postby Phil Haack (former manager on the MVC team)).

似乎正斜杠不会自动编码,原因可能是即使它们被编码 (%2f),当它们到达路由引擎时,它们已被解码回正斜杠。(在Phil Haack(MVC 团队的前经理)的这篇文章中搜索 Robj )。

However, .NET MVC Routing w/ Url Encoding Problemsposes the same problem, and it appears that the only way to solve this is to insert the encoded slash into a query string. Something like this:

然而,.NET MVC Routing w/Url Encoding Problems提出了同样的问题,看来解决这个问题的唯一方法是将编码的斜杠插入查询字符串中。像这样的东西:

< a href="@Url.Action("Details", "Search")[email protected]("a/b")">Details </a>

and then, dealing with it in your method by accessing:

然后,通过访问在您的方法中处理它:

Request["id"]

回答by Lazarus

You should use an escaped URL, so that parameter would be a%2Fb

您应该使用转义的 URL,以便该参数为 a%2Fb