【问题标题】:Adding a Http-Header in a HttpModule and read it out from a Page在 HttpModule 中添加 Http-Header 并从页面中读取
【发布时间】:2010-10-06 11:17:48
【问题描述】:

我尝试编写自己的 HttpModule (IHttpModule),添加这样的 Header:

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"];

但它没有用。知道为什么吗?

【问题讨论】:

    标签: c# .net asp.net http-headers httpmodule


    【解决方案1】:

    您正在向将从服务器发送到客户端的标头添加一些内容,并尝试从服务器已经从客户端接收到的标头中读取它。这是两个完全不同的集合。

    如果您使用它在模块和页面之间进行通信,您可能会发现最好在HttpContext.Items 中添加一些内容,这样可以传递各种对象,并且不会污染标题那里不相关的东西,也不需要会话,因此这是在同一请求上运行的代码之间进行通信的好方法。

    【讨论】:

      【解决方案2】:

      像这样添加,使用事件“EndRequest”

      void application_EndRequest(object sender, EventArgs e)
      {
                  HttpResponse response = HttpContext.Current.Response;
                  response.AddHeader("Author", "Sam Lin");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多