How to modify Request.Headers["Referer"] when performing Response.Redirect?
In the web app (C#, ASP.NET) I am working on at the moment,开发者_如何转开发 the value in Request.Headers["Referer"] can determine things like custom style. I have created a custom page with a drop down menu to test this functionality. So when the selected index changes, the value selected should be set in the Request.Headers["Referer"] then will be redirected (Response.Redirect), the receiving page will then pick up the value in Request.Headers["Referer"] and adjust the styling accordingly. However I haven't been able to set value for Request.Headers["Referer"]. Is it possible at all?
Website 1 sets the value in Request.Headers["Referer"], e.g. www.xyz.com and before doing Response.Redirect to www.website2.com
Website 2 picks up value in Request.Headers["Referer"], in this case www.xyz.com and do what it needs to do, i.e. styling etc.
try this Code
In Website 1 put this code in Button Click
HttpRequest equest = this.Request;
NameValueCollection headers = Request.Headers;
//get a type
Type t = headers.GetType();
t.InvokeMember("MakeReadWrite", BindingFlags.InvokeMethod |
BindingFlags.NonPublic | BindingFlags.Instance, null, headers, null);
t.InvokeMember("InvalidateCachedArrays", BindingFlags.InvokeMethod |
BindingFlags.NonPublic | BindingFlags.Instance, null, headers, null);
System.Collections.ArrayList item = new System.Collections.ArrayList();
item.Add("YOUR_STYLE_NAME");
t.InvokeMember("BaseAdd", BindingFlags.InvokeMethod | BindingFlags.NonPublic |
BindingFlags.Instance, null, headers, new object[] { "CUSTOM_STYLE", item });
t.InvokeMember("MakeReadOnly", BindingFlags.InvokeMethod |
BindingFlags.NonPublic | BindingFlags.Instance, null, headers, null);
Server.Transfer("Default.aspx");
and in Website 2 put this code to read style
var bnymuser = Request.Headers["CUSTOM_STYLE"];
You can't do this. The Request.Headers["Referer"] value is a value sent by the browser on each request. It's up to the browser what value it choose to supply for this value, and there is no means for a web page to send a response that says "for your next request, use this value for the Referer". And when you do a Request.Redirect, you're sending a response to the browser, telling it to make another request.
So whatever you're trying to achieve, this attempted method is not going to be the way to do it.
use query string to easily perform the same task.
You can simply not do this the way you're trying to do.
Why ??
The Request.Headers in this case, are the request headers received from the web-client - in most cases a browser. If you need to modify or add headers of an Http Request you first need to act as an HttpClient, just like the browser does. Once the request reaches the Http-server, in your case your website 1, you can not modify its values because its not allowed to do so. Because of this reason you cannot modify the request headers and then redirect to website 2 with modified headers. you'll have to give up either of the thing.
As others have mentioned you can redirect to website 2 with values in query-string.
精彩评论