如何在 ASP .NET MVC 中对参数进行 URL 编码

发布于 2024-11-05 22:39:15 字数 656 浏览 1 评论 0原文

我认为有以下代码:

<%= Html.ActionLink(
           "View item", 
           "Index", 
            "Items", 
            new 
            { 
                itemName = Model.ItemName 
            }, 
            null) %>

当项目名称包含尖号 (#) 或百分号 (%) 时,我遇到问题。

  • 当项目名称为“name#with#sharp#”时,控制器仅接收名称的第一部分,直到第一个锐号(仅接收“name”)。

  • 当项目名称为“name%with%percent”时,我收到错误:HTTP 错误 400 - 错误请求。

我不确定如果这是 URL 编码的问题,因为它可以与其他编码一起使用冲突的字符,例如:

;
=
+
,
~
[blank]

你知道我该如何解决这个问题吗?

提前致谢。

I have the following code in my view:

<%= Html.ActionLink(
           "View item", 
           "Index", 
            "Items", 
            new 
            { 
                itemName = Model.ItemName 
            }, 
            null) %>

I have a problem when the item name contains a sharp (#) or the percent symbol (%).

  • When the item name is "name#with#sharp#", the controller receives only the first part of the name until the first sharp (only receives "name").

  • When the item name is "name%with%percent" I get an error: HTTP error 400 - Bad request.

I not sure if this is a problem with the URL encoding, because it works with other conflictive chars such as:

;
=
+
,
~
[blank]

Do you know how could I address this issue?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑白记忆 2024-11-12 22:39:15

我假设您有一个路由设置,并且您的网址如下所示:

http://localhost/ Items/Index/name%25with%25percent - (这会爆炸)

与此相反:

http://localhost/Items/Index/?itemName=name%25with%25percent - (查询字符串可以)

因此,一个选项是从路由中删除“itemName”属性(在您的 RouteCollection 中),以便 Html.ActionLink 将使用 itemName 作为 QueryString 参数来呈现 Url。

正如@Priyank所说,问题是因为itemName是Url的一部分(不是QueryString参数)并且它包含非法字符。

I'm assuming you have a route setup and your url looks something like this:

http://localhost/Items/Index/name%25with%25percent - (this will blow up)

as opposed to this:

http://localhost/Items/Index/?itemName=name%25with%25percent - (query string is ok)

So an option would be to remove the "itemName" property from your route (in your RouteCollection) so that Html.ActionLink will render the Url using itemName as a QueryString parameter.

As @Priyank says, the problem is because the itemName is part of the Url (not a QueryString parameter) and it contain illegal characters.

被翻牌 2024-11-12 22:39:15

由于这些路由值作为 URL 字符串的一部分发布,因此它们将被视为单独的值,并以 # 和 % 分隔。有几个选项可以处理您的案件。

您必须实现自定义 ValueProvider(IValueProvider,尤其是 RouteDataValueProvider) 来处理您的自定义需求。一位程序员遇到了字符“/”的问题,他在这里破解了它 http://mrpmorris.blogspot.com/2012/08/asp-mvc-encoding-route-values.html

其次是将值存储在 TempData 中,该值在两个请求中持续存在并使用它们。

希望这有助于朝正确的方向思考。

Since these routedvalues are posted as part of URL string, they will be treated as separate values, separated by # and %. There are couple options for handle your case.

You will have to implement your custom ValueProvider (IValueProvider and especially RouteDataValueProvider) to handle your custom need. One programmer had an issue with character '/' and he hacked it here http://mrpmorris.blogspot.com/2012/08/asp-mvc-encoding-route-values.html

Second is to store values in TempData which persists across two request and use them.

Hope this helps to think in right direction.

戈亓 2024-11-12 22:39:15

您应该能够仅使用视图的 UrlHelper 实例来为您执行此操作。尝试一下:

<%= Html.ActionLink(
“查看项目”,
“指数”,
“项目”,
新的
{
itemName = Url.Encode(Model.ItemName)
},
null) %>

更新

经过测试,似乎像我上面那样显式编码似乎不太准确,并且会导致服务器双重编码(例如 - % 将在 url 中显示为 %2525)。

You should be able to just use the UrlHelper instance of your view to do this for you. Try giving this a shot:

<%= Html.ActionLink(
"View item",
"Index",
"Items",
new
{
itemName = Url.Encode(Model.ItemName)
},
null) %>

Update

After testing, it seems explicitly encoding like I did above seems to be less accurate and will cause the server to double-encode (eg - % will come out as %2525 in the url).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文