循环遍历 VB.NET 中的 request.querystring

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

Looping through a request.querystring in VB.NET

vb.netquery-string

提问by plntxt

I am trying to loop through a query string and pull out certain values as in:

我正在尝试遍历查询字符串并提取某些值,如下所示:

?ProductID=1234&ProductID=4321&Quantity=1

For each value next to ProductID I want to execute some logic. But I am not sure how to get to the values. Any ideas?

对于 ProductID 旁边的每个值,我想执行一些逻辑。但我不确定如何获得这些值。有任何想法吗?

回答by Jason DeFontes

When your query string has more than one value with the same key you can use the NameValueCollection.GetValuesmethod which returns a string array:

当您的查询字符串具有多个具有相同键的值时,您可以使用NameValueCollection.GetValues方法返回一个字符串数组:

dim productID as string
for each productID  in Page.Request.QueryString.GetValues("ProductID")
  ' do something with productID
next productID  

回答by James

Here is some untested psuedo code that should work for the code behind on the page. I hope this helps.

这是一些未经测试的伪代码,它们应该适用于页面背后的代码。我希望这有帮助。

dim key as string
dim list as new arraylist()
for each key in Page.Request.QueryString.Keys
 if key = "ProductID" then
   list.add(Page.Request.QueryString(key))
 end if
next key

' do somthing with the list of product id's

回答by Chris Ballance

Dim productID = Request.Querystring("ProductID")
Dim quantity = Request.Querystring("Quantity")

回答by madcolor

Dim sQS as String = Request.QueryString.ToString
For Each eItem In Split(sQS, "&")
Dim sName As String = Left(eItem, InStr(eItem & "=", "=") - 1)
Response.Write(sName _
& " = " & Request.QueryString(sName) _
& "<br>")
Next

and this is shorter but based on the same idea

这是更短但基于相同的想法

For Each Key As String In Request.QueryString.Keys
Response.Write(Key & " = " & Request.QueryString(Key) & "<br>")
Next

回答by JaredPar

Try this one. Only works in VB9.

试试这个。仅适用于 VB9。

Dim queryString = GetQueryString()
queryString = queryString.SubString(1) 'Remove ?
Dim ids = queryString. _
  Split("&"c). _
  Select(Function(x) x.Split("="c)). _
  Where(Function(x) x(0) = "ProductId" ). _
  Select(Function(x) x(1))

回答by PurTahan

For read Valueof get Parameteruse Request.QueryString.Item("param")

对于读Valueget Parameter使用Request.QueryString.Item("param")