How to get value of QueryString inside QueryString

Discussion in 'ASP .Net' started by Mehdi, Apr 6, 2006.

  1. Mehdi

    Mehdi Guest

    Hi,

    I get the following URL on page load:

    http://www.server1.com?RedirectUrl=http://www.server2.com?id=777

    So QueryString["RedirectUrl"] will return (after cleaning up via URL
    Encode/decode) :
    "http://www.server2.com?id=777"

    However I am interested to get the value of "777" without using string
    functions.

    Any suggestions?



    Kind Regards


    Mehdi
    Mehdi, Apr 6, 2006
    #1
    1. Advertising

  2. Mehdi

    Charlie@CBFC Guest

    You left off "&" delimiter...

    http://www.server1.com?RedirectUrl=http://www.server2.com&id=777

    string url = Request.QueryString["RedirectUrl"]
    string id = Request.QueryString["id"]

    Charlie

    "Mehdi" <> wrote in message
    news:...
    > Hi,
    >
    > I get the following URL on page load:
    >
    > http://www.server1.com?RedirectUrl=http://www.server2.com?id=777
    >
    > So QueryString["RedirectUrl"] will return (after cleaning up via URL
    > Encode/decode) :
    > "http://www.server2.com?id=777"
    >
    > However I am interested to get the value of "777" without using string
    > functions.
    >
    > Any suggestions?
    >
    >
    >
    > Kind Regards
    >
    >
    > Mehdi
    >
    Charlie@CBFC, Apr 6, 2006
    #2
    1. Advertising

  3. Mehdi

    Ray Booysen Guest

    Mehdi wrote:
    > Hi,
    >
    > I get the following URL on page load:
    >
    > http://www.server1.com?RedirectUrl=http://www.server2.com?id=777
    >
    > So QueryString["RedirectUrl"] will return (after cleaning up via URL
    > Encode/decode) :
    > "http://www.server2.com?id=777"
    >
    > However I am interested to get the value of "777" without using string
    > functions.
    >
    > Any suggestions?
    >
    >
    >
    > Kind Regards
    >
    >
    > Mehdi
    >
    >


    Use substring to retrieve all the chars after the ?. You can find the
    starting position of ? using the String static methods too.
    Ray Booysen, Apr 6, 2006
    #3
  4. Mehdi

    Mehdi Guest

    > You left off "&" delimiter...

    & has not be left off as the value of the "RedirectUrl" is a FULL url
    itself, and since there is only one querystring value in this url "?" is
    required instead of "&". In other words id=777 is not the QueryString of
    www.Server1.com

    Thanks for the suggestion anyway.

    Mehdi



    "Charlie@CBFC" <> wrote in message
    news:%...
    > You left off "&" delimiter...
    >
    > http://www.server1.com?RedirectUrl=http://www.server2.com&id=777
    >
    > string url = Request.QueryString["RedirectUrl"]
    > string id = Request.QueryString["id"]
    >
    > Charlie
    >
    > "Mehdi" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> I get the following URL on page load:
    >>
    >> http://www.server1.com?RedirectUrl=http://www.server2.com?id=777
    >>
    >> So QueryString["RedirectUrl"] will return (after cleaning up via URL
    >> Encode/decode) :
    >> "http://www.server2.com?id=777"
    >>
    >> However I am interested to get the value of "777" without using string
    >> functions.
    >>
    >> Any suggestions?
    >>
    >>
    >>
    >> Kind Regards
    >>
    >>
    >> Mehdi
    >>

    >
    >
    Mehdi, Apr 6, 2006
    #4
  5. Hi,

    Firstly, you might want to URL-encode the value of your RedirectUrl
    parameter, some characters aren't allowed in the querystring, including
    question marks. For more info about URL encoding and the
    HttpUtility.HtmlEncode(String) function, see MSDN:
    http://msdn2.microsoft.com/en-us/library/73z22y6h(VS.80).aspx

    Once you've got the encoded value of RedirectUrl from the QueryString
    collection, you might need to HtmlDecode() it to get back the original
    values:
    http://msdn2.microsoft.com/en-us/library/system.web.httputility.htmldecode(VS.80).aspx

    Next, you need to get the querystring from you URL value. The easiest
    way would be to use string functions to get everything to the right of
    the "?", but you wanted to avoid this. Therefore, another approach
    would be to create a Uri object from your URL string:
    http://msdn2.microsoft.com/en-US/library/system.uri(VS.80).aspx

    Uri myUrl = new Uri("http://www.server2.com?id=777");

    You can now get the querystring part of the URL using the Uri.Query
    property:

    string myQuery = myUrl.Query;

    Finally, how do you get the value of the id parameter without using
    string functions? You can use the HttpUtility.ParseQueryString()
    method to parse your querystring into a NameValueCollection object:
    http://msdn2.microsoft.com/en-US/library/ms150046.aspx

    NameValueCollection queryCol = HttpUtility.ParseQueryString(myQuery);
    idValue = queryCol["id"];

    Note that ParseQueryString() is only supported in .NET 2.0+.

    Hope this helps,
    Chris
    Chris Fulstow, Apr 6, 2006
    #5
  6. Mehdi

    Mehdi Guest

    Chris,

    Thanks for the help. It seems that I will end up using string functions to
    extract the id from MyUrl.Query. ParseQueryString() would have been the
    perfect solution if I had .NET 2.


    Regards

    Mehdi


    "Chris Fulstow" <> wrote in message
    news:...
    > Hi,
    >
    > Firstly, you might want to URL-encode the value of your RedirectUrl
    > parameter, some characters aren't allowed in the querystring, including
    > question marks. For more info about URL encoding and the
    > HttpUtility.HtmlEncode(String) function, see MSDN:
    > http://msdn2.microsoft.com/en-us/library/73z22y6h(VS.80).aspx
    >
    > Once you've got the encoded value of RedirectUrl from the QueryString
    > collection, you might need to HtmlDecode() it to get back the original
    > values:
    > http://msdn2.microsoft.com/en-us/library/system.web.httputility.htmldecode(VS.80).aspx
    >
    > Next, you need to get the querystring from you URL value. The easiest
    > way would be to use string functions to get everything to the right of
    > the "?", but you wanted to avoid this. Therefore, another approach
    > would be to create a Uri object from your URL string:
    > http://msdn2.microsoft.com/en-US/library/system.uri(VS.80).aspx
    >
    > Uri myUrl = new Uri("http://www.server2.com?id=777");
    >
    > You can now get the querystring part of the URL using the Uri.Query
    > property:
    >
    > string myQuery = myUrl.Query;
    >
    > Finally, how do you get the value of the id parameter without using
    > string functions? You can use the HttpUtility.ParseQueryString()
    > method to parse your querystring into a NameValueCollection object:
    > http://msdn2.microsoft.com/en-US/library/ms150046.aspx
    >
    > NameValueCollection queryCol = HttpUtility.ParseQueryString(myQuery);
    > idValue = queryCol["id"];
    >
    > Note that ParseQueryString() is only supported in .NET 2.0+.
    >
    > Hope this helps,
    > Chris
    >
    Mehdi, Apr 6, 2006
    #6
  7. Mehdi

    sloan Guest

    Maybe you can figure out what Microsoft is doing by going here:
    http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/fx/bcl/webclient_8cs-source.html



    "Mehdi" <> wrote in message
    news:...
    > Hi,
    >
    > I get the following URL on page load:
    >
    > http://www.server1.com?RedirectUrl=http://www.server2.com?id=777
    >
    > So QueryString["RedirectUrl"] will return (after cleaning up via URL
    > Encode/decode) :
    > "http://www.server2.com?id=777"
    >
    > However I am interested to get the value of "777" without using string
    > functions.
    >
    > Any suggestions?
    >
    >
    >
    > Kind Regards
    >
    >
    > Mehdi
    >
    >
    sloan, Apr 6, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mehdi
    Replies:
    2
    Views:
    402
    Mehdi
    Jan 29, 2005
  2. Replies:
    2
    Views:
    18,632
    JohnnyC
    Aug 8, 2011
  3. Frédéric Colombero
    Replies:
    0
    Views:
    1,093
    Frédéric Colombero
    Nov 5, 2003
  4. Eskimo
    Replies:
    0
    Views:
    149
    Eskimo
    Feb 7, 2005
  5. Adeel Ahmad
    Replies:
    1
    Views:
    429
    Anthony Jones
    Mar 7, 2006
Loading...

Share This Page