我正在开发一个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;
}
}
答案 0 :(得分:1)
有两种方法:
http://www.4guysfromrolla.com/articles/120308-1.aspx
处理应用程序的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\" />"));
}
}
}