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"]
Instead of passing the id through Headers, use Cookies.
Response.Cookies.Add(new HttpCookie("id", someId));
Response.Redirect("http://www.somesite.com/somepage.aspx");
You can get this id using the following code:
Request.Cookies["id"];
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>
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");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With