在 HttpModule 中添加 Http-Header 并从页面中读取它
我尝试编写自己的 HttpModule (IHttpModule),它添加了这样的标头:
public class MyModule: IHttpModule
{
public void Init(HttpApplication c)
{
c.BeginRequest += delegate{c.Response.AddHeader("MyHeader", "MyValue");};
}
public void Dispose(){}
}
并尝试在 aspx 页面中读取这样的内容:
var x = Request.ServerVariables["MyHeader"];
但它不起作用。知道为什么吗?
I've tried to write my own HttpModule (IHttpModule) that adds a Header like that:
public class MyModule: IHttpModule
{
public void Init(HttpApplication c)
{
c.BeginRequest += delegate{c.Response.AddHeader("MyHeader", "MyValue");};
}
public void Dispose(){}
}
and tried to read in a aspx page like that:
var x = Request.ServerVariables["MyHeader"];
but it didn't work. Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在向将从服务器发送到客户端的标头添加一些内容,并尝试从服务器已从客户端接收到的标头中读取它。这是两个完全不同的集合。
如果您使用它在模块和页面之间进行通信,您可能会发现最好向 HttpContext.Items 添加一些内容,这允许传递各种对象,并且不会用不相关的内容污染标头,也不需要会话,因此这是在同一请求上运行的代码之间进行通信的好方法。
You're adding something to the headers that will be sent from the server to the client and trying to read it from the headers already received by the server from the client. These are two completely different collections.
If you are using this to communicate between the module and the page, you may find it preferable to add something to the
HttpContext.Items
, this allows for all sorts of objects to be passed, and doesn't pollute the headers with stuff that aren't relevant there, nor require sessions, so it is a good way to communicate between code operating on the same request.像这样添加它,使用事件“EndRequest”
add it like this , use event "EndRequest"