How to add and access Custom Header in C#

2019-07-07 15:18发布

I need to add a custom header something like MYName: Balaji which i need to access from .aspx file through Request.ServerVariables["HTTP_MYName"]; should return "Balaji". I need so many variables like this it will added dynamically. Kindly help.

Also, I cannot persist this varaibles in any of the .Net controls or objects like cookies, sessions, application, hidden variable etc., or cannot store this in d/b and get it back whenever is required, I NEED IT ONLY IN HTTP HEADERS.

Kindly send the C# code how to add this variable and get the value back in .aspx file.

标签: http header
1条回答
我想做一个坏孩纸
2楼-- · 2019-07-07 15:24

What do you mean by "get the value back in .aspx file"? HTTP headers are intended to be used as directives to a browser, how to interpret the given content. You don't have access to these values in your document.

Setting a custom HTTP header is quite easy, however:

   Page.Response.AddHeader("MyCustomHeader", "VerySecretValue")

Updated my answer as per your comment. If you need to transfer information between a HTTPModule and an ASPX page, you can use HTTPContext.Current, since this stays the same in both places. So, you add it by

  HttpContext.Current.Items.Add("SecretKey", "SecretValue");

and read it as

  string s = HttpContext.Current.Items["SecretKey"];
查看更多
登录 后发表回答