在HTTPModule中捕获和修改CSS文件

时间:2011-10-28 13:27:49

标签: stream httprequest httpmodule httpresponse response.filter

在HTTP请求期间,我正在尝试使用 HTTPModule Response.Filter 拦截和修改CSS文件。这是因为我需要动态更改CSS类中的一些图像路径。

public class RequestValidationModule : IHttpModule
{
   private HttpApplication application;

   public void Init(HttpApplication app)
   {
     app.ReleaseRequestState += (new EventHandler(this.application_ReleaseRequestState));
     application = app;
   }

   public void application_ReleaseRequestState(object sender, EventArgs e)
   {
     string absolutePath = application.Request.Url.AbsolutePath;

     if (absolutePath.EndsWith(".css", StringComparison.OrdinalIgnoreCase) && absolutePath.IndexOf("/css/") > -1)
     {
        HttpResponse response = application.Response;

        if (response.ContentType.StartsWith("text/html", StringComparison.OrdinalIgnoreCase))
        {
            response.Filter = new UpdateCssFilterStream(response.Filter, response.ContentEncoding, "/images", "/sitename");
        }
     }
    }
}

事实证明,Filter属性只允许修改HTML页面结果的内容。我无法修改任何其他文件,如Javascript和CSS。

有人知道在HTTP请求期间捕获和更改CSS文件的另一种方法吗?

谢谢,

[]'RPG

0 个答案:

没有答案