我正在尝试编写一个 IIS 扩展,该扩展可以在请求发送回客户端时修改请求的响应 HTML。我知道传统上这将是 ISAPI 过滤器的工作,但我是一名 .Net 开发人员,并且我读过在 .Net 中实现 ISAPI 过滤器 不好想法。
我在一些地方读到,IIS 7 闪亮的新集成管道模式现在应该允许我在 .Net 中编写模块,通过实现 IHttpModule,但是我不知道如何修改非 ASP.Net 请求的响应 HTML。
我找到的关于如何执行此操作的唯一资源是 这篇文章 但这似乎不起作用对于非 ASP.Net 资源或调用 HttpApplication.CompleteRequest
的情况,即使启用了集成管道模式也是如此。
如何用 C# 编写 IIS 模块,允许我修改所有请求的响应 html/数据?
I'm trying to write an IIS extension that modifies the response HTML for a request as it is sent back to the client. I know that traditionally this would be the work of an ISAPI filter however I'm a .Net developer and I've read that implementing an ISAPI filter in .Net is a bad idea.
I have read in a few places that the shiny new integrated pipeline mode of IIS 7 should now allow me to writes modules in .Net that provide the same functionality as ISAPI filters by implementing IHttpModule, however I can't work out how to modify the response HTML for non ASP.Net requests.
The only resource I have found on how I might do this is this article however this doesn't appear to work for non ASP.Net resources or in cases where HttpApplication.CompleteRequest
is called, even if the integrated pipeline mode is enabled.
How can I write an IIS module in C# that allows me to modify the response html / data for all requests?
发布评论
评论(2)
我认为您必须通过 ASP.NET 模块路由所有请求。
I think you would have to route all your requests through the ASP.NET module.
不确定这对于 .NET 是否真的可行,但请查看此 http://aspnetresources.com/articles/CustomErrorPages 特别是。关于安装 HttpModule 的部分 - 也许这更多的是 IIS 如何看待你的模块的问题。
尽管我同意 .NET-ISAPI-Filter 是一个坏主意,但我仍然会检查这个看看这对您的情况有多大影响。
一个真正困难的选择是实现一个本地代理(即您可以编写一个基于 HttpListener 的服务器或使用一个库来简化实现),该代理侦听要公开的端口并重新配置 IIS 以使用其他端口/本地主机只是......这样你就可以过滤任何你想要的......尽管这也会影响性能恕我直言。
Not sure that this is really feasable with .NET but checkout this http://aspnetresources.com/articles/CustomErrorPages esp. the part about installing the HttpModule - perhaps this is more a matter of how IIS sees your module.
Although I am with you that a .NET-ISAPI-Filter is a bad idea I would still check this out to see how much impact this has in your case.
A really hard option would be to implement a local proxy (i.e. either you can write a HttpListener-based server or use a library to ease implemenation) which listens on the port(s) to be exposed and reconfigure IIS to use other ports/localhost only... this way you can filter whatever you want... although that will impact performance too IMHO.