Accessing URL encoded querystring vars

Discussion in 'ASP .Net' started by C., Apr 29, 2010.

  1. C.

    C. Guest

    I should know this already, but I'm exhausted and I was out of coffee
    this morning :)

    A have a pager that allows me to page through a dataset, and I print
    off some links at the bottom so users can jump between pages.

    When I build the links, I URL encode the hrefs so I get XHTML-
    compliant pages:

    StringBuilder queryString = new StringBuilder();
    queryString.Append("adminID=" + adminID);
    queryString.Append("&userPage=" + userPage);
    return Server.URLEncode(String.Format("Home.aspx?{0}",
    queryString.ToString()));

    This produces: <a href="/Home.aspx%3fadminID%3d1%26userPage%3d2">2</a>

    When I click that link, I get a terse Bad Request message (no
    exception, no stack trace, just "Bad Request")

    I tried HTML Encoding the URL, so I end up with:
    <a href="/Home.aspx?adminID=1&amp;userPage=2>2</a>

    Unfortunately, when the link is clicked, the querystring variables is
    not retrieved as expected.

    if (!Int32.TryParse(Request.QueryString["userPage"], out userPage))
    {
    //This part I really don't want to run, but I do when I am
    parsing a URL Encoded querystring
    userPage = 1
    }

    Looking at the Querystring object, the key of interest is
    "amp;userPage", not "userPage" as expected. I presume I need to run
    decode the key, but I'm kinda flummoxed as to how to do that.

    I could assign the Querystring to a NameValueCollection, tearing out
    the offending "amp;" prefix as I do, but that doesn't seem especially
    elegant. Is there a cleaner way to achieve what i want?

    Cheers,

    C.
     
    C., Apr 29, 2010
    #1
    1. Advertising

  2. C.

    C. Guest

    Sorry, I didn't double encode my html-encoded example so it won't
    display correctly in browsers. After I HTML encode my link, it looks
    like: <a href="/Home.aspx?adminID=1&amp;amp;userPage=2>2</a>
     
    C., Apr 29, 2010
    #2
    1. Advertising

  3. On Apr 29, 3:29 pm, "C." <> wrote:
    > I should know this already, but I'm exhausted and I was out of coffee
    > this morning :)
    >
    > A have a pager that allows me to page through a dataset, and I print
    > off some links at the bottom so users can jump between pages.
    >
    > When I build the links, I URL encode the hrefs so I get XHTML-
    > compliant pages:
    >
    > StringBuilder queryString = new StringBuilder();
    > queryString.Append("adminID=" + adminID);
    > queryString.Append("&userPage=" + userPage);
    > return Server.URLEncode(String.Format("Home.aspx?{0}",
    > queryString.ToString()));
    >
    > This produces: <a href="/Home.aspx%3fadminID%3d1%26userPage%3d2">2</a>
    >
    > When I click that link, I get a terse Bad Request message (no
    > exception, no stack trace, just "Bad Request")
    >
    > I tried HTML Encoding the URL, so I end up with:
    > <a href="/Home.aspx?adminID=1&userPage=2>2</a>
    >
    > Unfortunately, when the link is clicked, the querystring variables is
    > not retrieved as expected.
    >
    > if (!Int32.TryParse(Request.QueryString["userPage"], out userPage))
    >         {
    >             //This part I really don't want to run, but I do when I am
    > parsing a URL Encoded querystring
    >             userPage = 1
    >         }
    >
    > Looking at the Querystring object, the key of interest is
    > "amp;userPage", not "userPage" as expected. I presume I need to run
    > decode the key, but I'm kinda flummoxed as to how to do that.
    >
    > I could assign the Querystring to a NameValueCollection, tearing out
    > the offending "amp;" prefix as I do, but that doesn't seem especially
    > elegant. Is there a cleaner way to achieve what i want?
    >
    > Cheers,
    >
    > C.


    Request.QueryString doesn't use XML encoding, it uses URL encoding. So
    you either need to need to get QueryString.ToString() and parse it or
    you can also try to replace '&' (&amp;) in the url by %26
     
    Alexey Smirnov, Apr 29, 2010
    #3
  4. C. wrote:
    > I should know this already, but I'm exhausted and I was out of coffee
    > this morning :)
    >
    > A have a pager that allows me to page through a dataset, and I print
    > off some links at the bottom so users can jump between pages.
    >
    > When I build the links, I URL encode the hrefs so I get XHTML-
    > compliant pages:
    >
    > StringBuilder queryString = new StringBuilder();
    > queryString.Append("adminID=" + adminID);
    > queryString.Append("&userPage=" + userPage);
    > return Server.URLEncode(String.Format("Home.aspx?{0}",
    > queryString.ToString()));


    You need to UrlEncode the values of the parameters and HtmlEncode the other
    bits, so instead of using "&" for additional parameters, use "&amp;":

    StringBuilder queryString = new StringBuilder();
    queryString.Append("adminID=" + Server.UrlEncode(adminID));
    queryString.Append("&amp;userPage=" + Server.UrlEncode(userPage) );
    return String.Format("Home.aspx?{0}",queryString.ToString());

    or it's probably more obvious as one line:

    return String.Format("Home.aspx?adminID={0}&amp;userPage={2}",
    Server.UrlEncode(adminID), Server.UrlEncode(userPage))

    --
    Andrew
     
    Andrew Morton, Apr 29, 2010
    #4
  5. Andrew Morton wrote:

    > or it's probably more obvious as one line:
    >
    > return String.Format("Home.aspx?adminID={0}&amp;userPage={2}",
    > Server.UrlEncode(adminID), Server.UrlEncode(userPage))


    Oops! {1}, not {2}.

    --
    Andrew
     
    Andrew Morton, Apr 29, 2010
    #5
    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. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    400
  2. Ken Fine
    Replies:
    1
    Views:
    418
    Walter Wang [MSFT]
    Jan 22, 2008
  3. Linuxguy123
    Replies:
    7
    Views:
    699
    Paddy O'Loughlin
    Feb 20, 2009
  4. caccolangrifata
    Replies:
    18
    Views:
    400
    Chris Torek
    Jul 22, 2011
  5. Adeel Ahmad
    Replies:
    1
    Views:
    538
    Anthony Jones
    Mar 7, 2006
Loading...

Share This Page