English 中文(简体)
在HttpModule中添加Http头并从页面中读取
原标题:Adding a Http-Header in a HttpModule and read it out from a Page

我试着写我自己的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"];

但没有奏效。知道为什么吗?

最佳回答

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

如果您使用它在模块和页面之间进行通信,您可能会发现最好在<code>HttpContext.Items</code>中添加一些东西,这样可以传递各种对象,并且不会用与之无关的东西污染头部,也不需要会话,因此这是在处理同一请求的代码之间进行通信的好方法。

问题回答

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

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签