adding an item to a QueryString

P

PJ6

I thought this was going to be simple. I want to take an HttpRequest and add
an item to the QueryString.

First I tried to add it directly, but I got a "Collection is read-only."
error. So I decided to construct my own, new HttpRequest based on the
contents of the old one (from within a class that inherits from Page):

Dim q As New
System.Collections.Specialized.NameValueCollection(Me.Request.QueryString)
q.Add("TEST", test)
Dim r As New HttpRequest(Me.Request.FilePath, Me.Request.Url.ToString,
q.ToString)

Only this doesn't work, q.ToString returns
"System.Collections.Specialized.NameValueCollection". Do I really have to
serialize the QueryString myself? Is there really no built-in method to do
this?

I'm sure I misinterpreted what the constructor wants for FileName, but the
documentation offers no comment. Why is there no corresponding property?

Paul
 
P

Patrice

Try perhaps Response.Redirect if you want to direct the browser to a new
page and/or with a new querystring...
Else explain us what you want to do...

(you can't change the current request, it is what it is and you just handle
this).
 
P

PJ6

Try perhaps Response.Redirect if you want to direct the browser to a new
page and/or with a new querystring...

And - to repeat my qestion - can one build this new query string using the
built-in object model, or does the logic for this have to be coded by hand?

Paul
 
J

Joerg Jooss

PJ6 said:
I thought this was going to be simple. I want to take an HttpRequest
and add an item to the QueryString.

First I tried to add it directly, but I got a "Collection is
read-only." error. So I decided to construct my own, new HttpRequest
based on the contents of the old one (from within a class that
inherits from Page):

Dim q As New
System.Collections.Specialized.NameValueCollection(Me.Request.QueryStr
ing) q.Add("TEST", test) Dim r As New
HttpRequest(Me.Request.FilePath, Me.Request.Url.ToString, q.ToString)

Only this doesn't work, q.ToString returns
"System.Collections.Specialized.NameValueCollection". Do I really
have to serialize the QueryString myself? Is there really no built-in
method to do this?

It doesn't really make a lot of sense to change a property of a request
you've already received (except debugging and testing).

Any object you might want to associate with a particular request at
this point in time can be passed by SessionState or HttpContext.

Cheers,
 
I

intrader

And - to repeat my qestion - can one build this new query string using the
built-in object model, or does the logic for this have to be coded by hand?

Paul
Not that I know of. However, why don't you render a client-side hidden
field? It will be included in the QueryString
 
F

Ferret

It doesn't really make a lot of sense to change a property of a request
you've already received (except debugging and testing).

Any object you might want to associate with a particular request at
this point in time can be passed by SessionState or HttpContext.

Actually, it makes perfect sense in certain circumstances. Let's say
you want to change the behavior of an entire site in some universal way
without having to re-code the entire thing. Let's say you want to be
able to send in one querystring parm to the site, say "Mode2", and have
the site use a different stylesheet if "Mode2" is in the Querystring.

Well, that's all fine and dandy, but what happens the second you click
a link on the site? That parm and its value will be lost and the site
will switch back to the other stylesheet. If there was a way to check
the referrer and add the querystring to the current request if it was
present in the referrer, the value could be propagated at a high level,
and no other code would need to be changed. It would be like a
persistent querystring item - once present, always present. I wanted
to do this to provide my users a way to surf their site using a totally
different stylesheet without having to re-code the entire site to
handle new functionality.

I can think of other uses for this as well, but alas I haven't found a
good way to do it.
 
F

Ferret

Well, writing that description got me thinking on the problem again,
and I solved it the best way I can think of. For my specific scenario,
I just put a check at the very beginning of Application_BeginRequest to
look for the parm. If it's not found, I check the referrer. If it's
found there, I immediately do a redirect, adding the parm.

This works well as long as the referrer is intact. Unfortunately, I've
got a third party navigation control that's clearing it out.
 
J

Joerg Jooss

Ferret said:
Actually, it makes perfect sense in certain circumstances. Let's say
you want to change the behavior of an entire site in some universal
way without having to re-code the entire thing. Let's say you want
to be able to send in one querystring parm to the site, say "Mode2",
and have the site use a different stylesheet if "Mode2" is in the
Querystring.

Well, that's all fine and dandy, but what happens the second you click
a link on the site? That parm and its value will be lost and the site
will switch back to the other stylesheet. If there was a way to check
the referrer and add the querystring to the current request if it was
present in the referrer, the value could be propagated at a high
level, and no other code would need to be changed. It would be like a
persistent querystring item - once present, always present. I wanted
to do this to provide my users a way to surf their site using a
totally different stylesheet without having to re-code the entire
site to handle new functionality.

All of that can be easily done using cookies or a some object in the
user's session. Heck, you could even do that with query strings. Of
course not for the current request, but all subsequent ones -- append
the key Mode2 to each and every URL you render in your pages... ouch ;-)

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top