473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

HttpContext.Current.Request.QueryString

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
Jun 27 '08 #1
12 14748
Peter wrote:
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?
Shouldn't the query string be: "localhost?a=&b=&c=123" ?

FB

--
------------------------------------------------------------------------
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#)
------------------------------------------------------------------------
Jun 27 '08 #2
Frans Bouma [C# MVP] wrote:
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?

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
Jun 27 '08 #3
On Apr 25, 5:35*am, "Peter" <xdz...@hotmail.comwrote:
Frans Bouma [C# MVP] wrote:
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?
* *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
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;
Jun 27 '08 #4
Ignacio Machin ( .NET/ C# MVP ) wrote:
On Apr 25, 5:35*am, "Peter" <xdz...@hotmail.comwrote:
Frans Bouma [C# MVP] wrote:
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?
* *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

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;
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
Jun 27 '08 #5
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
"Peter" wrote:
Ignacio Machin ( .NET/ C# MVP ) wrote:
On Apr 25, 5:35 am, "Peter" <xdz...@hotmail.comwrote:
Frans Bouma [C# MVP] wrote:
>
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?
>
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
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;

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
Jun 27 '08 #6
Peter Bromberg [C# MVP] wrote:
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
Jun 27 '08 #7
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
"Peter" wrote:
Peter Bromberg [C# MVP] wrote:
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
Jun 27 '08 #8
Peter Bromberg [C# MVP] wrote:
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
Jun 27 '08 #9
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
"Peter" wrote:
Peter Bromberg [C# MVP] wrote:
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
Jun 27 '08 #10
Peter Bromberg [C# MVP] wrote:
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
Jun 27 '08 #11
On Apr 25, 1:01*pm, "Peter" <xdz...@hotmail.comwrote:
Peter Bromberg [C# MVP] wrote:
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
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.
Jun 27 '08 #12
Ignacio Machin ( .NET/ C# MVP ) wrote:
On Apr 25, 1:01*pm, "Peter" <xdz...@hotmail.comwrote:
Peter Bromberg [C# MVP] wrote:
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

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.
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
Jun 27 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: MarkMurphy | last post by:
In a barebones website with one page, checking HttpContext.Current.Request.UserLanguages.Length in Page_Load of an ASPX page in the VS IDE in codebehind shows a value of 1. Then when I refresh...
2
by: Francois Malgreve | last post by:
hello guys, I have some helper class in my ASP.NET pplication who basically contains static methods. I used them as helper methods to do small jobs that I can use at many places in my code. ...
7
by: Keith Patrick | last post by:
After completely giving up on finding some way in my ASP.Net app to take a query string URL and redirect to it as a POST instead, I went with a system like so: Upon "redirection," all the...
1
by: josephpage | last post by:
I have a Visitor class that is defined in a class library. The class has lots of parameters that are pulled from the QueryString, the ReferrerUrl, Cookies, the Session object, etc... I would...
3
by: Steve Franks | last post by:
Is there a way I can extend the HttpContext or one of its subclasses to include a property that exposes a custom class of mine to all ASP.NET pages? More specifically, I'd like to use a...
2
by: Dave | last post by:
After some digging, I discovered HttpContext.Current.Session is null when trying to access a session variable, username, in my upload.cs code which is in the App_Code folder. I just determined...
4
by: msch-prv | last post by:
Hello. I am trying to use caching to populate a datalist. The select method of the associated objectdatasource calls up GetRecipePageByRecipeCatID() to request the proper data (I am using custom...
3
by: Brett R. Wesoloski | last post by:
I am having problems using HttpContext.Current.Request.Url.Host. I have some code that does this... if (HttpContext.Current != null) { subdomain = HttpContext.Current.Request.Url.Host; }
3
by: Madhur | last post by:
Hello I am delivering an asp.net 2.0 application to my customer. I need to know, If I need to check for the condition of HttpContext.Current to be null in my business logic. I have extensively...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.