PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Accessing URL encoded querystring vars

 
 
C.
Guest
Posts: n/a
 
      29th Apr 2010
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.
 
Reply With Quote
 
 
 
 
C.
Guest
Posts: n/a
 
      29th Apr 2010
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>
 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      29th Apr 2010
On Apr 29, 3:29*pm, "C." <hardi...@hotmail.com> 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
 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      29th Apr 2010
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


 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      29th Apr 2010
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there an existing .NET method for combining querystring vars correctly? Ken Fine Microsoft ASP .NET 1 22nd Jan 2008 07:52 AM
Pivot tables - can change row/col vars but can't get rid of existing vars Andreww Microsoft Excel Programming 0 25th May 2007 11:50 AM
Protect input data stored in external files (or Deserialize encoded xml file? (serialized and encoded)) Magnus Microsoft C# .NET 2 5th Apr 2006 11:27 AM
Pass Javascript vars to global vars in asp.net rbutch@coair.com Microsoft ADO .NET 1 21st Mar 2005 06:39 AM
app vars and cache vars Jon Microsoft ASP .NET 3 14th Dec 2004 08:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:48 PM.