我正在尝试循环遍历查询字符串并提取特定的值,例如:
?ProductID=1234&ProductID=4321&Quantity=1
针对ProductID旁边的每个值,我想执行一些逻辑。但是我不确定如何获取这些值。有什么想法吗?
我正在尝试循环遍历查询字符串并提取特定的值,例如:
?ProductID=1234&ProductID=4321&Quantity=1
针对ProductID旁边的每个值,我想执行一些逻辑。但是我不确定如何获取这些值。有什么想法吗?
当你的查询字符串有多个相同的键值时,你可以使用NameValueCollection.GetValues方法,它会返回一个字符串数组:
dim productID as string
for each productID in Page.Request.QueryString.GetValues("ProductID")
' do something with productID
next productID
以下是一些未经测试的伪代码,应该可以用于页面后台代码。希望这能帮到您。
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
Dim productID = Request.Querystring("ProductID")
Dim quantity = Request.Querystring("Quantity")
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))
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
如果要读取get参数
的值
,请使用Request.QueryString.Item("param")