NameValueCollection 到 URL 查询?
我知道我可以做到这一点
var nv = HttpUtility.ParseQueryString(req.RawUrl);
,但是有没有办法将其转换回网址?
var newUrl = HttpUtility.Something("/page", nv);
I know i can do this
var nv = HttpUtility.ParseQueryString(req.RawUrl);
But is there a way to convert this back to a url?
var newUrl = HttpUtility.Something("/page", nv);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
只需在
NameValueCollection
上调用ToString()
即可以name1=value1&name2=value2
查询字符串就绪格式返回名称值对。请注意,NameValueCollection
类型实际上并不支持这一点,并且建议这样做会产生误导,但由于实际返回的内部类型,该行为在这里起作用,如下所述。感谢 @mjwills 指出
HttpUtility.ParseQueryString
方法实际上返回一个内部HttpValueCollection
对象,而不是常规NameValueCollection
(尽管文档指定了NameValueCollection
)。使用ToString()
时,HttpValueCollection
自动对查询字符串进行编码,因此无需编写循环遍历集合并使用UrlEncode
的例程方法。期望的结果已经返回。有了结果,您就可以将其附加到 URL 并重定向:
目前使用
HttpValueCollection
的唯一方法是使用上面显示的ParseQueryString
方法(除了当然是反思)。自从 请求公开此类的连接问题 已关闭,状态为“无法修复”。顺便说一句,您可以调用
nameValues
上的Add
、Set
和Remove
方法来修改任何在附加查询字符串项目之前。如果您对此感兴趣请参阅我对另一个问题的回复。Simply calling
ToString()
on theNameValueCollection
will return the name value pairs in aname1=value1&name2=value2
querystring ready format. Note thatNameValueCollection
types don't actually support this and it's misleading to suggest this, but the behavior works here due to the internal type that's actually returned, as explained below.Thanks to @mjwills for pointing out that the
HttpUtility.ParseQueryString
method actually returns an internalHttpValueCollection
object rather than a regularNameValueCollection
(despite the documentation specifyingNameValueCollection
). TheHttpValueCollection
automatically encodes the querystring when usingToString()
, so there's no need to write a routine that loops through the collection and uses theUrlEncode
method. The desired result is already returned.With the result in hand, you can then append it to the URL and redirect:
Currently the only way to use a
HttpValueCollection
is by using theParseQueryString
method shown above (other than reflection, of course). It looks like this won't change since the Connect issue requesting this class be made public has been closed with a status of "won't fix."As an aside, you can call the
Add
,Set
, andRemove
methods onnameValues
to modify any of the querystring items before appending it. If you're interested in that see my response to another question.创建一个使用几个循环的扩展方法。我更喜欢这个解决方案,因为它可读(无 linq),不需要 System.Web.HttpUtility,并且它支持重复键。
示例
输出
Make an extension method that uses a couple of loops. I prefer this solution because it's readable (no linq), doesn't require System.Web.HttpUtility, and it supports duplicate keys.
Example
Output
这应该不需要太多代码即可工作:
其想法是使用 HttpUtility.ParseQueryString 生成类型为 HttpValueCollection 的空集合。此类是
NameValueCollection
的子类,被标记为internal
,因此您的代码无法轻松创建它的实例。HttpValueCollection
的好处是ToString
方法会为您处理编码。通过利用NameValueCollection.Add(NameValueCollection)
方法,您可以将现有查询字符串参数添加到新创建的对象中,而无需先将Request.QueryString
集合转换为url 编码的字符串,然后将其解析回集合中。该技术也可以作为扩展方法公开:
This should work without too much code:
The idea is to use
HttpUtility.ParseQueryString
to generate an empty collection of typeHttpValueCollection
. This class is a subclass ofNameValueCollection
that is marked asinternal
so that your code cannot easily create an instance of it.The nice thing about
HttpValueCollection
is that theToString
method takes care of the encoding for you. By leveraging theNameValueCollection.Add(NameValueCollection)
method, you can add the existing query string parameters to your newly created object without having to first convert theRequest.QueryString
collection into a url-encoded string, then parsing it back into a collection.This technique can be exposed as an extension method as well:
实际上,您也应该对键进行编码,而不仅仅是值。
Actually, you should encode the key too, not just value.
由于
NameValueCollection
同一键可以有多个值,因此如果您关心查询字符串的格式(因为它将作为逗号分隔值而不是“数组表示法”返回),您可以考虑下列。示例
变为:
key1=val1&key2[]=val2&empty&key2[]=val2b
而不是key1=val1&key2=val2,val2b&empty
。代码
或者如果你不太喜欢 Linq...
正如评论中已经指出的那样,除了 这个答案< /a> 大多数其他答案都解决了这种情况(
Request.QueryString
是一个HttpValueCollection
,“不是”NameValueCollection
),而不是文字问题。更新:解决了评论中的空值问题。
Because a
NameValueCollection
can have multiple values for the same key, if you are concerned with the format of the querystring (since it will be returned as comma-separated values rather than "array notation") you may consider the following.Example
Turn into:
key1=val1&key2[]=val2&empty&key2[]=val2b
rather thankey1=val1&key2=val2,val2b&empty
.Code
or if you don't like Linq so much...
As has been pointed out in comments already, with the exception of this answer most of the other answers address the scenario (
Request.QueryString
is anHttpValueCollection
, "not" aNameValueCollection
) rather than the literal question.Update: addressed null value issue from comment.
简短的答案是在
NameValueCollection
上使用.ToString()
并将其与原始 url 结合起来。不过,我想指出一些事情:
您不能在
Request.RawUrl
上使用HttpUtility.ParseQueryString
。ParseQueryString()
方法正在查找如下所示的值:?var=value&var2=value2
。如果您想获取
QueryString
参数的NameValueCollection
,只需使用Request.QueryString()
。要重建 URL,只需使用 nv.ToString()。
如果您尝试解析 url 字符串而不是使用
Request
对象,请使用Uri
和HttpUtility.ParseQueryString
方法。The short answer is to use
.ToString()
on theNameValueCollection
and combine it with the original url.However, I'd like to point out a few things:
You cant use
HttpUtility.ParseQueryString
onRequest.RawUrl
. TheParseQueryString()
method is looking for a value like this:?var=value&var2=value2
.If you want to get a
NameValueCollection
of theQueryString
parameters just useRequest.QueryString()
.To rebuild the URL just use nv.ToString().
If you are trying to parse a url string instead of using the
Request
object useUri
and theHttpUtility.ParseQueryString
method.我总是使用 UriBuilder 将带有查询字符串的 url 转换回有效且正确编码的 url。
I always use UriBuilder to convert an url with a querystring back to a valid and properly encoded url.
在 AspNet Core 2.0 中,您可以使用 QueryHelpers AddQueryString 方法。
In AspNet Core 2.0 you can use QueryHelpers AddQueryString method.
正如 @Atchitutchuk 建议的那样,您可以在 ASP.NET Core 中使用 QueryHelpers.AddQueryString:
As @Atchitutchuk suggested, you can use QueryHelpers.AddQueryString in ASP.NET Core:
这对我来说很有效:
This did the trick for me:
你可以使用。
如果此 nv 是字符串类型,您可以附加到 uri 第一个参数。
喜欢
You can use.
if this nv is of type string you can append to the uri first parameter.
Like