Accessing URL encoded querystring vars

  • Thread starter Thread starter C.
  • Start date Start date
C

C.

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.
 
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>
 
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
 
C. said:
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 said:
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}.
 
Back
Top