3

I have this url : http://localhost:49500/Learning/Chapitre.aspx?id=2

How can I get just the value of id in this url ?

Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174
  • 1
    Are you parsing the URL as a string, or are you running code from making a request to that URL? – womp Jun 17 '10 at 00:00
  • I have another page that redirect to this page – Wassim AZIRAR Jun 17 '10 at 00:02
  • This was asked and answerd at: http://stackoverflow.com/questions/2884551/get-individual-query-parameters-from-uri – Peter Jun 17 '10 at 00:05
  • that doesn't really answer the question, although I'll assume the latter. IN that case, Patricker's answer is correct. – womp Jun 17 '10 at 00:14
  • The question should probably have been phrased a little differently, I think it was a little confusing. You were trying to get the a query string value that had been passed to the CURRENT page, it wasn't that you just had a URL as a string laying around. I would probably go back and edit the question so other people can benefit from it more easily. – Peter Jun 17 '10 at 00:21

4 Answers4

5

You can access all the query strings through the Request.QueryString() array:

Request.QueryString("id") will give you the 2

WTFZane
  • 592
  • 1
  • 4
  • 25
Jaime
  • 6,736
  • 1
  • 26
  • 42
2

Despite my own comment saying it has been answered, here is the code.

Dim idval As String = System.Web.HttpUtility.ParseQueryString("http://localhost:49500/Learning/Chapitre.aspx?id=2")("id")
Peter
  • 9,643
  • 6
  • 61
  • 108
0

Create a new instance of System.Uri class with the URL and the use the Query property to get the query string part.

Once you have that string, do String.Split on the '&' character. For each string in the resulting array, do String.Split on the '=' char. In the resulting array, the first string is the query parameter name, the second is the value (if present). Check if the name is the one you are interested in and if it is, get the value.

Update: Boy, I haven't touched VB since 1999... :-)

Here's the code for my answer. I didn't realize that the Url you want to parse is the page Url. For that specific case, Request.QueryString("id") will indeed be a better solution.

    Dim url As Uri = New Uri("http://localhost:49500/Learning/Chapitre.aspx?id=2")
    Dim query As String = url.Query.Trim("?")
    Dim parameters() As String = query.Split("&")
    Dim tokens() As String
    Dim value As String = ""
    For index As Integer = 0 To parameters.Length - 1
        tokens = parameters(index).Split("=")
        If tokens(0).ToLower = "id" Then
            If tokens.Length = 2 Then
                value = tokens(1)
            End If
            Exit For
        End If
    Next
    ' At this point value contains the parameter value or
    ' is empty if the parameter has no value or if the parameter is not present
Franci Penov
  • 74,861
  • 18
  • 132
  • 169
0

You can use Request vb method Using the url : http://localhost:49500/Learning/Chapitre.aspx?id=2

Dim valueId = Request("id")

to test the code:

response.Write(valueId)

value Id is 2

Kevin Kalitowski
  • 6,829
  • 4
  • 36
  • 52