Kat*_*teF 5 c# parsing query-string
我在sharepoint 2007页面上有一个C#custom webpart.在另一个页面上单击SSRS报告中的链接时,它会将用户发送到我的自定义webpart页面,其中包含如下查询字符串:
?tax4Elem=Docks%20&%20Chargers&ss=EU%20MOVEX&Phase=1&tax3Elem=Play%20IT&tax5Elem=Charger
Run Code Online (Sandbox Code Playgroud)
注意"tax4Elem"的价值,这基本上是"码头和充电器".(&符号实际上可以出现在"tax4Elem","tax3Elem"和"tax5Elem"中).
我不能在该值中编码&符号,所以我将不得不使用它.
如何解析此查询字符串,使其无法识别"码头和充电器"中的"&"作为键/值对的开头?
提前致谢!凯特
如果你确实无法更正 URL,你仍然可以尝试解析它,但你必须做出一些决定。例如:
=在键后面总是有一个等号&\w+)捕获这些对的一种可能方法是:
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);
Run Code Online (Sandbox Code Playgroud)
然后您可以获得值:
string tax4 = values["tax4Elem"];
Run Code Online (Sandbox Code Playgroud)
请注意,如果根据我们的规则查询字符串“无效”,则该模式可能无法捕获所有值。