简体   繁体   English

如何使用C#解析值中带有“&”的查询字符串?

[英]How do I parse a query string with “&” in the value using C#?

I have a C# custom webpart on a sharepoint 2007 page. 我在sharepoint 2007页面上有一个C#定制Webpart。 When clicking on a link in an SSRS report on another page, it sends the user to my custom webpart page with a query string like the following: 单击另一页上的SSRS报告中的链接时,它将使用查询字符串将用户发送到我的自定义Webpart页面,如下所示:

?tax4Elem=Docks%20&%20Chargers&ss=EU%20MOVEX&Phase=1&tax3Elem=Play%20IT&tax5Elem=Charger

Take note of the value for "tax4Elem", which is basically "Docks & Chargers". 注意“ tax4Elem”的值,它基本上是“ Docks&Chargers”。 (The ampersand can actually come up in "tax4Elem", "tax3Elem", and "tax5Elem"). (“&”号实际上可以出现在“ tax4Elem”,“ tax3Elem”和“ tax5Elem”中)。

I cannot have the ampersand in that value encoded so I will have to work with this. 我不能在该值中编码与号,因此我将不得不对此进行处理。

How do I parse this query string so that it doesn't recognize the "&" in "Docks & Chargers" as the beginning of a key/value pair? 如何解析此查询字符串,以使它无法将“码头和充电器”中的“&”识别为键/值对的开头?

Thanks in Advance! 提前致谢! kate 凯特

I think you can't parse that string correctly - it has been incorrectly encoded. 我认为您无法正确解析该字符串-编码错误。 The ampersand in "Docks & Chargers" should have been encoded as %26 instead of & : “基座和充电器”中的&字符应编码为%26而不是&

?tax4Elem=Docks%20%26%20Chargers&ss=EU%20MOVEX&Phase=1&tax3Elem=Play%20IT&tax5Elem=Charger

Is it possible to change the code that generated the URL? 是否可以更改生成URL的代码?

Obviously the request is incorrect. 显然,该请求是不正确的。 However, to work-around it, you can take the original URL, then find the IndexOf of &ss= . 但是,要解决此问题,可以采用原始URL,然后找到&ss=的IndexOf。 Then, find the = sign immediately before that. 然后,在此之前找到=符号。 Decode (with UrlDecode ) then reencode (with UrlEncode ) the part between the = and &ss= (the value of tax4Elem). 解码(使用UrlDecode ),然后重新编码(使用UrlEncode=&ss= (tax4Elem的值)之间的部分。 Then, reconstruct the query string like this: 然后,像这样重建查询字符串:

correctQueryString = "?tax4Elem=" + reencodedTaxValue + remainderOfQueryString

and decode it normally (eg with ParseQueryString ) into a NameValueCollection . 并将其正常解码(例如,使用ParseQueryString )为NameValueCollection

If you really cannot correct the URL, you can still try to parse it, but you have to make some decisions. 如果您确实无法纠正该URL,则仍然可以尝试解析该URL,但是您必须做出一些决定。 For example: 例如:

  • Keys can only contain alphanumeric characters. 键只能包含字母数字字符。
  • There are no empty values, or at least, there is always an equal sign = after the key 没有空值,或者至少在键后始终有一个等号=
  • Values may contain additional ampersands and question marks. 值可能包含其他与号和问号。
  • Values may contain additional equal signs, as long as they don't appear to be part of a new key/value pair (they are not preceded with &\\w+ ) 值可能包含其他等号,只要它们看起来不属于新键/值对的一部分(它们不以&\\w+

One possible way to capture these pairs is: 捕获这些对的一种可能方法是:

MatchCollection matches = Regex.Matches(s, @"\G[?&](?<Key>\w+)=(?<Value>.*?(?=$|&\w+=))");
var values = matches.Cast<Match>()
                    .ToDictionary(m => m.Groups["Key"].Value,
                                  m => HttpUtility.UrlDecode(m.Groups["Value"].Value),
                                  StringComparer.OrdinalIgnoreCase);

You can then get the values: 然后,您可以获取值:

string tax4 = values["tax4Elem"];

Note that if the query string is "invalid" according to our rule, the pattern may not capture all values. 请注意,如果根据我们的规则查询字符串为“无效”,则该模式可能不会捕获所有值。

或者,您可以使用HttpServerUtility.HtmlDecode方法将值解码为'&'(与号)符号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM