【问题标题】:Looping through a request.querystring in VB.NET在 VB.NET 中遍历 request.querystring
【发布时间】:2009-02-18 21:36:38
【问题描述】:

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

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

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

【问题讨论】:

    标签: vb.net query-string


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      这里有一些未经测试的伪代码,应该适用于页面后面的代码。我希望这会有所帮助。

      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
      

      【讨论】:

        【解决方案3】:
        Dim productID = Request.Querystring("ProductID")
        Dim quantity = Request.Querystring("Quantity")
        

        【讨论】:

        • VB 不支持方括号,那是 C#。
        【解决方案4】:

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

        【讨论】:

          【解决方案5】:
          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
          

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-11-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-01-11
              相关资源
              最近更新 更多