Response.Redirect

  • Thread starter Thread starter Peter Kirk
  • Start date Start date
P

Peter Kirk

Hi

I need to write a method which accepts a url (string) and redirects to it.
In addition to this I need to either (a) add a parameter to the url; or (b)
add the parameter to the header of the redirect request.

To add a parameter I guess it's as easy as adding "?myparam=myvalue" to the
url (or "&myparam=myvalue" if there already are parameters in the url string
I get).

But how do I add a header? Can I use Request.Headers.Add("myparam",
myvalue), and then do a Redirect?

Thanks,
Peter
 
Peter,

When you redirect, a 302 response is sent back to the client. It is the
client that then gets the URL to redirect to, and the client which
subsequently makes the request. Because of this, you can't change the
headers that are sent.

What you want to do is call the Transfer method on the HttpServerUtility
(the Server property on the page, or the HttpContext) to transfer processing
to another page. You can opt to keep the form properties that were sent in
the initial request.

However, you will not be able to append any values to the Form values
sent with the request. While Form returns a NameValueCollection, it does
not allow you to add elements to it once it is populated.

Rather, what you should do is encode the information in the URL, or have
a key that you append to the URL which points to something in session state
that your page can retreive.

Hope this helps.
 
Nicholas Paldino said:
When you redirect, a 302 response is sent back to the client. It is
the client that then gets the URL to redirect to, and the client which
subsequently makes the request. Because of this, you can't change the
headers that are sent.

What you want to do is call the Transfer method on the
HttpServerUtility (the Server property on the page, or the HttpContext) to
transfer processing to another page. You can opt to keep the form
properties that were sent in the initial request.

However, you will not be able to append any values to the Form values
sent with the request. While Form returns a NameValueCollection, it does
not allow you to add elements to it once it is populated.

Rather, what you should do is encode the information in the URL, or
have a key that you append to the URL which points to something in session
state that your page can retreive.

Thanks for your reply. I'm not sure I can use your advice however.

Maybe if I restate the actual problem I am trying to solve, then someone can
give some advice on how to tackle it (rather than me assuming a particular
method of solving the problem).

I have a web-user-control which needs to "call" an external url (or transfer
control to that url... or whatever it is called in the web world). My
control receives some parameters from which it determines what external url
to call. My control also receives some other parameters which need to be
sent on to the external url.

The external urls expect to receive the parameters either like a "get
request": fx http://www.e-data.xx?myparam=myvalue; or they may expect to
found the parameter values in the request header.

Any advice on tackling this?


Thanks,
Peter
 
Peter,

In this case, you have to use the GET method to form the particular URL,
and use a redirect for the reasons I stated regarding the browser making the
request. You can't tell it what headers to send, but you can tell it the
URL to go to.
 
Back
Top