在 HttpModule 中添加 Http-Header 并从页面中读取它

发布于 2024-09-26 03:48:55 字数 404 浏览 0 评论 0原文

我尝试编写自己的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青巷忧颜 2024-10-03 03:48:55

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

如果您使用它在模块和页面之间进行通信,您可能会发现最好向 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.

多像笑话 2024-10-03 03:48:55

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

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

add it like this , use event "EndRequest"

void application_EndRequest(object sender, EventArgs e)
{
            HttpResponse response = HttpContext.Current.Response;
            response.AddHeader("Author", "Sam Lin");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文