tempid
tempid

Reputation: 8208

Response.Redirect with headers

I'm trying to set the headers and redirecting to a different page like this -

Response.Headers.Add("id", "testtest");
Response.Redirect("http://www.somesite.com/somepage.aspx");

And in the page_load of somepage.aspx, I'm checking the request for headers -

if (!string.IsNullOrEmpty(Request["id"]))
{
   // do something with "id"
}

But Request["id"] is always null. How do I get the values of the header in the new page? I do not want to use query strings.

Thanks!

Update:

Here's a little more detail -- I have two ASP.NET v4 web applications (Site 1 and Site 2) running on two different machines. Site 1 has just one aspx form and it has only one button on it. On button click, I hit the database and get the value I need and should pass it on to Site 2. In the Global.asax of Site 2, I'll be reading the header information received from Site 1 and use the value.

Update #2:

I was able to get it to work --

 Response.Write(
                    string.Format(
                        @"<form action='{0}' id='test' method='POST'><input type='hidden' name='key' value={1} /></form>
                  <script type='text/javascript'>
                     document.getElementById('test').submit();
                  </script> ",
                        "http://www.somesite.com", "1234"));

In the destination site, I was able to get the value using -

Request["key"]

Upvotes: 17

Views: 64207

Answers (6)

Dhairyasheel Nikam
Dhairyasheel Nikam

Reputation: 41

Instead of passing the id through Headers, use Cookies.

Response.Cookies.Add(new HttpCookie("id", someId));
Response.Redirect("http://www.somesite.com/somepage.aspx");
 

Consider to set to true httpCookie.HttpOnly and .Secure.

You can get this id using the following code:

Request.Cookies["id"];

Upvotes: 4

Aspiiire
Aspiiire

Reputation: 408

In my case none of these solutions worked, I Found a way that let me use the Response contructor and redirect by using the http protocol.

new Response(null, {                                                                                                               
   status: 307,                                                                                                                          
   headers: {                                                                                                                            
      'Set-Cookie': `myCookie=432556; Path=/; HttpOnly; Secure; SameSite=Strict`,                                               
      Location: '/',                                                                                                                    
   },                                                                                                                                    
});

Upvotes: 0

Arman
Arman

Reputation: 5316

There's no way to add headers to the http request but initiating one by either a user action through a browser or other programs or by sending such a request on your own.

So you could simply make use of one of those http clients in the.Net Framework like the HttpClient or HttpWebRequest and send a custom request with whichever headers you want instead of calling Response.Redirect.

Yet it's possible to add custom headers to http Response message only in IIS. Still no way to control the consequent request headers when performing a redirect.

<configuration>
   <system.webServer>
      <httpProtocol>
         <redirectHeaders>
            <add name="X-Custom-Redirect-Header" value="MyRedirectValue" />
         </redirectHeaders>
      </httpProtocol>
   </system.webServer>
</configuration>

Upvotes: 5

jwheron
jwheron

Reputation: 2562

Have you tried Server.Transfer("http://www.somesite.com/somepage.aspx");? This might get you the data you need. If not, you'll have to post the data to the form on your originating page if you don't want to use the Session variable.

Upvotes: 0

Srikanth Remani
Srikanth Remani

Reputation: 121

Response.Headers.Add("id", "testtest"); is not having the expected effect, because you have never sent Response to the client. If you are using Response.Redirect, you are simply redirecting to the url and the Request object is not hyderated with your previous Response params.

You can use some form AppContext/Session mechanism to pass params between these two pages.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038790

HTTP headers are valid only for the current response. When you set a redirect the current response contains your custom header but when the browser follows the redirect location those headers are no longer present. Furthermore you are using Request["id"] in the other page so you need to sent the value as query string:

Response.Redirect("http://www.somesite.com/somepage.aspx?id=test");

Upvotes: 19

Related Questions