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

4

我正在尝试循环遍历查询字符串并提取特定的值,例如:

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

针对ProductID旁边的每个值,我想执行一些逻辑。但是我不确定如何获取这些值。有什么想法吗?

6个回答

7

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

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

5

以下是一些未经测试的伪代码,应该可以用于页面后台代码。希望这能帮到您。

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

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

VB不支持方括号,这是C#的特性。 - James

1
尝试这一个。仅适用于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))

1
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

这是一个更短的但基于相同思路的内容

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

0

如果要读取get参数,请使用Request.QueryString.Item("param")


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接