Looping through a request.querystring in VB.NET

后端 未结 6 2083
我寻月下人不归
我寻月下人不归 2020-12-18 08:53

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

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

For each va

相关标签:
6条回答
  • 2020-12-18 09:16
    Dim productID = Request.Querystring("ProductID")
    Dim quantity = Request.Querystring("Quantity")
    
    0 讨论(0)
  • 2020-12-18 09:25
    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
    
    0 讨论(0)
  • 2020-12-18 09:33

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

    dim productID as string
    for each productID  in Page.Request.QueryString.GetValues("ProductID")
      ' do something with productID
    next productID  
    
    0 讨论(0)
  • 2020-12-18 09:35

    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
    
    0 讨论(0)
  • 2020-12-18 09:36

    For read Value of get Parameter use Request.QueryString.Item("param")

    0 讨论(0)
  • Try this one. Only works in 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))
    
    0 讨论(0)
提交回复
热议问题