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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5901049/
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:10:40  来源:igfitidea点击:

How to URL encode parameters in ASP .NET MVC

asp.net-mvcasp.net-mvc-2urlencode

提问by Daniel Pe?alba

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.

  • 当物品名称为 时"name#with#sharp#",控制器只接收名称的第一部分,直到第一个尖锐(只接收"name")。

  • 当项目名称是"name%with%percent"我得到一个错误:HTTP 错误 400 - 错误的请求。

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

我不确定这是否是 URL 编码的问题,因为它适用于其他冲突字符,例如:

;
=
+
,
~
[blank]

Do you know how could I address this issue?

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

Thanks in advance.

提前致谢。

回答by Lee Gunn

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

我假设你有一个路由设置,你的 url 看起来像这样:

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

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

as opposed to this:

与此相反:

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

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

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.

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

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

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

回答by Priyank

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.

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

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

您将必须实施您的自定义ValueProvider (IValueProvider and especially RouteDataValueProvider)来处理您的自定义需求。一位程序员遇到了字符“/”的问题,他在这里破解了它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.

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

Hope this helps to think in right direction.

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

回答by JasonOffutt

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

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

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

<%= 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 lessaccurate and will cause the server to double-encode (eg - % will come out as %2525 in the url).

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