开发者

Using HttpModules to modify the response sent to the client

I have two production websites that have similar content. One of these websites needs to be indexed by search engines and the other shouldn't. Is there a way of adding content to the response given to the 开发者_如何学运维client using the HttpModule?

In my case, I need the HttpModule to add to the response sent to the when the module is active on that particular web.


You'd probably want to handle the PreRequestHandlerExecute event of the application as it is run just before the IHttpHandler processes the page itself:

public class NoIndexHttpModule : IHttpModule
{
  public void Dispose() { }

  public void Init(HttpApplication context)
  {
    context.PreRequestHandlerExecute += AttachNoIndexMeta;
  }

  private void AttachNoIndexMeta(object sender, EventArgs e)
  {
    var page = HttpContext.Current.CurrentHandler as Page;
    if (page != null && page.Header != null)
    {
      page.Header.Controls.Add(new LiteralControl("<meta name=\"robots\" value=\"noindex, follow\" />"));
    }
  }
}

The other way of doing it, is to create your own Stream implementation and apply it through Response.Filters, but that's certainly trickier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜