从查询字符串中检索特定值. [英] Retrieve specific values from a query string.
问题描述
查询字符串是
the query string is
<a href="http://beta.firago.com/contact_us.aspx?item_name_1=Aito&item_quantity_1=11&item_price_1=875&item_options_1=pageLink%3A+aito.html%2C+bedrooms%3A+2+x+1+Bedroom&item_name_2=Peto&item_quantity_2=1&item_price_2=875&item_options_2=pageLink%3A+aito.html%2C+bedrooms%3A+2+x+1+Bedroom&return=success.html&cancel_return=cancel.html#.T-EgUMV5dXY">
上面的值涉及不同的产品,并且可能会有很多产品使它成为更长的查询字符串.
具体来说,我想检索值item_name_1和item_name_2以及3、4、5等(如果存在).
我知道我可以使用request.querystring("item_name_1")来获取上面的Aito值.
但是我猜我可能需要一个for循环来提取所有
的值
即类似
的东西
The values above relate to different products and there is the possiblity that there could be many products making it an even longer query string.
Specifically I would like to retrieve the values item_name_1 and item_name_2 and 3,4,5 etc if these are present.
I know i can use request.querystring("item_name_1") to get the value Aito above.
However I''m guessing I need perhaps a for loop to extract the values of all
ie something along the lines of
(for i = 0; i < request.querystring.count; i++)
{
txtboxbody.text = request.querystring("item_name_" + i);
}
上面的似乎不起作用.任何指导将不胜感激.
谢谢
The above does not seem to work however. Any guidance would be appreciated.
Thanks
推荐答案
以下内容应该可以帮助您确定要执行的操作:
The following should help you in figuring out what you want to do:
var str = @"http://beta.firago.com/contact_us.aspx?item_name_1=Aito&item_quantity_1=11&item_price_1=875&item_options_1=pageLink%3A+aito.html%2C+bedrooms%3A+2+x+1+Bedroom&item_name_2=Peto&item_quantity_2=1&item_price_2=875&item_options_2=pageLink%3A+aito.html%2C+bedrooms%3A+2+x+1+Bedroom&return=success.html&cancel_return=cancel.html#.T-EgUMV5dXY";
var re = new Regex(@"item_name_\d[^&]*=([^&]*)&");
var matches = re.Matches(str);
foreach (var m in matches)
Console.WriteLine(m);
var match = re.Match(str);
int matchCount = 0;
while (match.Success)
{
Console.WriteLine("Match" + (++matchCount));
for (int i = 1; i <= 2; i++)
{
Group g = match.Groups[i];
Console.WriteLine("Group" + i + "='" + g + "'");
CaptureCollection cc = g.Captures;
for (int j = 0; j < cc.Count; j++)
{
Capture c = cc[j];
System.Console.WriteLine("Capture" + j + "='" + c + "', Position=" + c.Index);
}
}
match = match.NextMatch();
}
Console.ReadLine();
许多代码来自 http://msdn.microsoft.com/zh-CN/library/twcw2f1c. aspx [^ ]
Much of code is taken from http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx[^]
我认为您可以通过array string
和split
通过&
处理它
然后使用for
loop ..我不知道我是否错了..如果是,请告诉我..
谢谢
灰烬
I think you can handle it througharray string
andsplit
it through&
then usefor
loop..I dont know if I am wrong..tell me if so..
Thanks
Ashish
这篇关于从查询字符串中检索特定值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!