2

我正在开发一个 IIS 模块,当发出网页请求时,它会查看传递回浏览器的数据,并用批准的关键字替换某些关键字。我意识到有多种方法可以做到这一点,但就我们的目的而言,IIS 模块效果最好。

如何将发送回浏览器的数据流读取为字符串,以便根据需要转换关键字?

任何帮助将不胜感激!

这是代码:

namespace MyNamespace
{
    class MyModule : IHttpModule
    {
        private HttpContext _current = null;

        #region IHttpModule Members

        public void Dispose()
        {
            throw new Exception("Not implemented");
        }

        public void Init(HttpApplication context)
        {
            _current = context.Context;

            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        #endregion

        public void context_PreRequestHandlerExecute(Object source, EventArgs e)
        {
            HttpApplication app = (HttpApplication)source;
            HttpRequest request = app.Context.Request;
        }
}
4

1 回答 1

1

有两种方法:

  1. 使用响应过滤器

http://www.4guysfromrolla.com/articles/120308-1.aspx

  1. 在处理页面本身PreRequestHandlerExecute之前处理应用程序的事件:IHttpHandler

    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\" />"));
        }
      }
    

    }

于 2011-08-29T16:02:11.007 回答