Ok..here is the solution...
you have to create custom httpmodule
add handler to begin request method..
in begin request method...get all headers and set readonly only property to false through reflection..
add custom header in NameValueCollection-->NameObjectCollectionBase' objtec's collection which ultimately add the header in request object's header..
Following is the code of httpmodule..
public class MyModule1 : IHttpModule
{
public void Dispose()
{
//clean-up code here.
}
public void Init(HttpApplication context) {
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
var headers = HttpContext.Current.Request.Headers;
Type hdr = headers.GetType();
PropertyInfo ro = hdr.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy);
// Remove the ReadOnly property
ro.SetValue(headers, false, null);
// Invoke the protected InvalidateCachedArrays method
hdr.InvokeMember("InvalidateCachedArrays",
BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
null, headers, null);
// Now invoke the protected "BaseAdd" method of the base class to add the
// headers you need. The header content needs to be an ArrayList or the
// the web application will choke on it.
hdr.InvokeMember("BaseAdd", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, headers, new object[] { "CustomHeaderKey", new ArrayList { "CustomHeaderContent" } });
// repeat BaseAdd invocation for any other headers to be added
// Then set the collection back to ReadOnly
ro.SetValue(headers, true, null);
}
}
following is the web.config setting needs to add
<httpModules >
<add name="MyModule1" type="WebApplication70515.MyModule1"/>
</httpModules>
aspx page on load code to see if the header is set or not
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Headers["CustomHeaderKey"] != null)
{
Response.Write(Request.Headers["CustomHeaderKey"]);
}
}
Please mark as answer if the above solution is helped to you.......
Hmanjarawala, if this helps please login to Mark As Answer. | Alert Moderator