IIS模块和C#:如何在发送给客户端之前修改页面内容
IIS module and C#: how to modify the page content before sending it to client
我是 IIS 模块和 C# 领域的新手。
在网站将页面内容发送给客户端之前,我需要修改网站特定目录中的静态HTML 文件的内容。修改包括添加横幅、页脚等
根据我的研究,我应该能够通过 IIS 模块实现我的目标(对吗?)。这是我的代码:
namespace MyProject
{
public class MyModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreSendRequestContent +=
new EventHandler(onPreSendRequestContent);
}
#endregion
public void onPreSendRequestContent(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpRequest request = app.Context.Request;
HttpResponse response = app.Context.Response;
if (request.Path.Contains("my_specific_directory"))
{
//add banner and footer to the page content
//which class, methods, or properties to use?
}
}
}
}
我不确定 PreSendRequestContent 是否是开始修改页面内容的正确事件。有人能告诉我正确的方法来获取 IIS 检索的页面内容吗?
感谢和问候。
我认为使用 MVC 框架更好,因为它易于维护并且您可以做任何事情。但是,如果您仍想通过 IIS HTTP 模块修改静态 HTMLs,请执行以下过程。希望这有帮助。
首先,添加处理程序和构建提供程序以通过 IIS 处理静态 HTML 文件。
Web.config:
<system.webServer>
<handlers>
<add name="html" verb="GET,HEAD,POST,DEBUG" path="*.html" type="System.Web.UI.PageHandlerFactory" />
</handlers>
</system.webServer>
<system.web>
<compilation>
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
</compilation>
</system.web>
接下来,编写一个 HTTP 模块和一个过滤器来修改您的 HTML 内容。
HTTP 模块:
class ModifyContentModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += (o, e) =>
{
context.Response.Filter = new ModifyContentStream(context.Response.Filter);
};
}
}
过滤器:
public class ModifyContentStream : Stream
{
private Stream _base;
private MemoryStream _memoryBase = new MemoryStream();
public ModifyContentStream(Stream stream)
{
_base = stream;
}
public override void Write(byte[] buffer, int offset, int count)
{
_memoryBase.Write(buffer, offset, count);
}
public override void Flush()
{
// Get static HTML code
string html = Encoding.UTF8.GetString(_memoryBase.GetBuffer());
// Modify your HTML
// Sample: Replace absolute links to relative
Regex regex = new Regex("(href=\")http:\/\/www\.example\.com(\/[^\"']+\.[^\"']+\")");
Match match = regex.Match(html);
while (match.Success)
{
string oldValue = match.Value;
string newValue = match.Groups[1].Value + match.Groups[2].Value;
html = html.Replace(oldValue, newValue);
match = match.NextMatch();
}
// Flush modified HTML
byte[] buffer = Encoding.UTF8.GetBytes(html);
_base.Write(buffer, 0, buffer.Length);
_base.Flush();
}
#region Rest of the overrides
}
}
最后,将 HTTP 模块添加到您的 Web.config
Web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="ModifyContent" />
<add name="ModifyContent" type="ModifyContentModule.ModifyContentModule, ModifyContentModule" />
</modules>
<handlers>
<add name="html" verb="GET,HEAD,POST,DEBUG" path="*.html" type="System.Web.UI.PageHandlerFactory" />
</handlers>
</system.webServer>
我是 IIS 模块和 C# 领域的新手。
在网站将页面内容发送给客户端之前,我需要修改网站特定目录中的静态HTML 文件的内容。修改包括添加横幅、页脚等
根据我的研究,我应该能够通过 IIS 模块实现我的目标(对吗?)。这是我的代码:
namespace MyProject
{
public class MyModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreSendRequestContent +=
new EventHandler(onPreSendRequestContent);
}
#endregion
public void onPreSendRequestContent(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpRequest request = app.Context.Request;
HttpResponse response = app.Context.Response;
if (request.Path.Contains("my_specific_directory"))
{
//add banner and footer to the page content
//which class, methods, or properties to use?
}
}
}
}
我不确定 PreSendRequestContent 是否是开始修改页面内容的正确事件。有人能告诉我正确的方法来获取 IIS 检索的页面内容吗?
感谢和问候。
我认为使用 MVC 框架更好,因为它易于维护并且您可以做任何事情。但是,如果您仍想通过 IIS HTTP 模块修改静态 HTMLs,请执行以下过程。希望这有帮助。
首先,添加处理程序和构建提供程序以通过 IIS 处理静态 HTML 文件。
Web.config:
<system.webServer>
<handlers>
<add name="html" verb="GET,HEAD,POST,DEBUG" path="*.html" type="System.Web.UI.PageHandlerFactory" />
</handlers>
</system.webServer>
<system.web>
<compilation>
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
</compilation>
</system.web>
接下来,编写一个 HTTP 模块和一个过滤器来修改您的 HTML 内容。
HTTP 模块:
class ModifyContentModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += (o, e) =>
{
context.Response.Filter = new ModifyContentStream(context.Response.Filter);
};
}
}
过滤器:
public class ModifyContentStream : Stream
{
private Stream _base;
private MemoryStream _memoryBase = new MemoryStream();
public ModifyContentStream(Stream stream)
{
_base = stream;
}
public override void Write(byte[] buffer, int offset, int count)
{
_memoryBase.Write(buffer, offset, count);
}
public override void Flush()
{
// Get static HTML code
string html = Encoding.UTF8.GetString(_memoryBase.GetBuffer());
// Modify your HTML
// Sample: Replace absolute links to relative
Regex regex = new Regex("(href=\")http:\/\/www\.example\.com(\/[^\"']+\.[^\"']+\")");
Match match = regex.Match(html);
while (match.Success)
{
string oldValue = match.Value;
string newValue = match.Groups[1].Value + match.Groups[2].Value;
html = html.Replace(oldValue, newValue);
match = match.NextMatch();
}
// Flush modified HTML
byte[] buffer = Encoding.UTF8.GetBytes(html);
_base.Write(buffer, 0, buffer.Length);
_base.Flush();
}
#region Rest of the overrides
}
}
最后,将 HTTP 模块添加到您的 Web.config
Web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="ModifyContent" />
<add name="ModifyContent" type="ModifyContentModule.ModifyContentModule, ModifyContentModule" />
</modules>
<handlers>
<add name="html" verb="GET,HEAD,POST,DEBUG" path="*.html" type="System.Web.UI.PageHandlerFactory" />
</handlers>
</system.webServer>