Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

HttpContext.Current.Request.QueryString

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter - 25 Apr 2008 10:17 GMT
Hi

if I have a url/query like "localhost?a&b&c=123" then I thought I could
get these parameters and values by using
NameValueCollection query = HttpContext.Current.Request.QueryString;

But if I then try "query.AllKeys" I only get two keys, namely "null"
and "c".

Is this correct behaviour? Are "a" and "b" considered to be values for
a null key - and how do I get them from the collection?

Thanks,
Peter
Frans Bouma [C# MVP] - 25 Apr 2008 10:27 GMT
> Hi
>
[quoted text clipped - 7 lines]
> Is this correct behaviour? Are "a" and "b" considered to be values for
> a null key - and how do I get them from the collection?

    Shouldn't the query string be: "localhost?a=&b=&c=123" ?

        FB

Signature

------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

Peter - 25 Apr 2008 10:35 GMT
> > if I have a url/query like "localhost?a&b&c=123" then I thought I
> > could get these parameters and values by using
[quoted text clipped - 7 lines]
>
>     Shouldn't the query string be: "localhost?a=&b=&c=123" ?

Yes, I think you're right, the query should really look like that.

I guess I should ignore any null keys I get from AllKeys (what can I
use a null key for anyway?)

Thanks,
Peter
Ignacio Machin ( .NET/ C# MVP ) - 25 Apr 2008 15:11 GMT
> > > if I have a url/query like "localhost?a&b&c=123" then I thought I
> > > could get these parameters and values by using
[quoted text clipped - 15 lines]
> Thanks,
> Peter

Who is generating that query string?
As Frans mentioned it's wrong.

You should have default values for all your parameters, and then just
update those with values in the QS.
so all you have to do at the most is adding a if (key==null) continue;
Peter - 25 Apr 2008 15:27 GMT
> > > > if I have a url/query like "localhost?a&b&c=123" then I thought
> > > > I could get these parameters and values by using
[quoted text clipped - 24 lines]
> update those with values in the QS.
> so all you have to do at the most is adding a if (key==null) continue;

In my program I was using AllKeys, and looping over the keys doing some
processing. Unfortunately I did not take account of the fact that a key
could be null.

A user decided to edit the query string himself, and then we got an
exception.

I did just like you said, and now check if the key is null before
processing it.

I can't really see the point in AllKeys returning a null key, but there
you go...

/Peter
Peter Bromberg [C# MVP] - 25 Apr 2008 15:59 GMT
It is very common to see querystrings with null key values (e.g., &ua=&bg=1 )
rather than the key not being present. Just take a look at some google
searches to see examples of this.
-- Peter
To be a success, arm yourself with the tools you need and learn how to use
them.

Site: http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://ittyurl.net

> > > > > if I have a url/query like "localhost?a&b&c=123" then I thought
> > > > > I could get these parameters and values by using
[quoted text clipped - 39 lines]
>
> /Peter
Peter - 25 Apr 2008 17:02 GMT
> It is very common to see querystrings with null key values (e.g.,
> &ua=&bg=1 ) rather than the key not being present. Just take a look
> at some google searches to see examples of this.

In your example there are no "null keys". You have a key called "ua"
and a key called "bg".

The value for ua is empty, and the value for bg is 1.

If your query string was "&ua&bg=1" then AllKeys would return two keys:
null and "bg".

I don't really understand the point of a null key.

/Peter
Peter Bromberg [C# MVP] - 25 Apr 2008 17:53 GMT
There needs to be an equals (=) sign after each key name.
-- Peter
To be a success, arm yourself with the tools you need and learn how to use
them.

Site: http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://ittyurl.net

> > It is very common to see querystrings with null key values (e.g.,
> > &ua=&bg=1 ) rather than the key not being present. Just take a look
[quoted text clipped - 11 lines]
>
> /Peter
Peter - 25 Apr 2008 18:01 GMT
> There needs to be an equals (=) sign after each key name.

Yes indeed. I found that out. But what to do if a user enters the query
string in the browser....

It seems strange to me that (1) the browser (internet explorer) sends
an "illegal" query string; and (2) the AllKeys property returns a key
which is null - using a null key to retrieve a value from the
NameValueCollection results in an error of course.

/Peter
Peter Bromberg [C# MVP] - 25 Apr 2008 19:41 GMT
I think it would be pretty rare for a web application that expects a user to
be creating their "own" querystrings. You can type whatever gobbledegook you
want after a URI in the Address Bar of your browser, and the browser will
happily attempt to retrieve the resource for you.  Cheers.
-- Peter
To be a success, arm yourself with the tools you need and learn how to use
them.

Site: http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://ittyurl.net

> > There needs to be an equals (=) sign after each key name.
>
[quoted text clipped - 7 lines]
>
> /Peter
Peter - 26 Apr 2008 12:33 GMT
> I think it would be pretty rare for a web application that expects a
> user to be creating their "own" querystrings. You can type whatever
> gobbledegook you want after a URI in the Address Bar of your browser,
> and the browser will happily attempt to retrieve the resource for
> you.  

I don't know if we're talking/writing past each other here... but it
was never the idea that a user should write parameters in the url in
the browser. However, one of our users did (for their own nefarious
reasons), and that's when we discovered an error in our program.

The user wrote a query like "?a&b&c=123". It could very well be this is
an invalid query. The browser though, as you wrote, happily sent this
invalid query to our web-app.

The web-app received that invalid query, and as part of its normal
processing used the AllKeys property of
HttpContext.Current.Request.QueryString to loop over all the keys. This
caused an unhandled error in our program because we were not aware that
a key could be null.

We are now aware of this fact, and have corrected our program to not
attempt to process any null keys. I am still surprised that AllKeys can
return a key which is null, but that's just life I guess :-)

Thanks,
Peter
Ignacio Machin ( .NET/ C# MVP ) - 28 Apr 2008 15:01 GMT
> > There needs to be an equals (=) sign after each key name.
>
[quoted text clipped - 7 lines]
>
> /Peter

Hi,

Then you are playing with fire, just be ready for a var not to be
present, or as in your first post an incorrect QS.
Peter - 29 Apr 2008 12:26 GMT
> > > There needs to be an equals (=) sign after each key name.
> >
[quoted text clipped - 12 lines]
> Then you are playing with fire, just be ready for a var not to be
> present, or as in your first post an incorrect QS.

Yes, thanks, we've fixed our application now.

I guess QueryString interprets "a" and "b" in "localhost?a&b&c=123" as
values which don't have a parameter name.

I thought they'd be interpreted as parameter names which don't have any
values - but that would be "?a=&b=".

I may even use this at some stage if I can't think of a good name for a
parameter and I just want to send some values to my web-app.

Thanks,
Peter

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.