问题
I am implementing HttpModule for compressing request. Below is the codee for HttpModule:
public class Global : IHttpModule
{
public void Init(HttpApplication app)
{
app.PostReleaseRequestState += new EventHandler(app_PostReleaseRequestState);
}
void app_PostReleaseRequestState(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
// If gzip is supported then gzip it else if deflate compression is supported then compress in that technique.
if (acceptEncoding.Contains("gzip"))
{
// Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "gzip");
}
else if (acceptEncoding.Contains("deflate"))
{
// Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
context.Response.AppendHeader("Content-Encoding", "deflate");
}
}
It's able to intercept and compress js and css in the development web server but when i run it from IIS 5.1 it is not able to compress js and css files. Please help.
回答1:
Prior to IIS7, getting non-ASP.NET file types into the ASP.NET pipeline requires setting up a file type mapping to redirect those files through ISAPI. If you map *.js
and *.css
to be processed by ISAPI, your code should start running for those requests.
Here's an example of doing that in IIS6 (though you'll want to substitute *.js
and *.css
for *.asp
). If I remember correctly, 5.1's management interface is similar enough that ScottGu's example should still be helpful.
回答2:
I would make sure that .js and .css files are handled by the .NET framework.
The reference for IIS 7 and above can be found at iis.net/ConfigReference/system.webServer/handlers
Concerning IIS 6, you can check that js and css are handled under: Site settings / Home Directory / Application Settings / (Application Pool) Configuration / Mappings
回答3:
You are on the right track using HttpModule. However, in order to change HTML contents, you would use the HttpApplication.PostReleaseRequestState handler. In order to change resource files, you would use the HttpApplication.BeginRequest handler.
来源:https://stackoverflow.com/questions/4993208/httpmodule-does-not-intercept-js-and-css-files-in-iis-5-1