问题
i'm optimizing our company web site with seo optimization and yslow. but in yslow the ETAGS are F . I've gone through tens of web sites and tutorials and the best option was using an HTTP Module. I've done so and tried several modules but none shows results.maybe something in syntax is wrong or i'm registering it wrong .some say it is best to use app_PostReleaseRequestState instead of OnPreSendRequestHeaders because of a crash in heap.I've used both with no results. here it is : the file name is etag and it is in app-code folder
web config :
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add type="CompressionModule" name="CompressionModule"/>
<add type="ETags" name="ETags"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
</system.webServer>
and here is the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ETags
/// </summary>
public class ETags : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication app)
{
app.PostReleaseRequestState += new EventHandler(app_PostReleaseRequestState);
}
void app_PostReleaseRequestState(object sender, EventArgs e)
{
HttpContext.Current.Response.Headers.Remove("ETag");
HttpContext.Current.Response.Headers.Remove("Server");
HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
HttpContext.Current.Response.Headers.Remove("X-Powered-By");
}
//public void Init(HttpApplication context)
//{
// context.PreSendRequestHeaders += OnPreSendRequestHeaders;
//}
//void OnPreSendRequestHeaders(object sender, EventArgs e)
//{
// HttpContext.Current.Response.Headers.Remove("ETag");
// HttpContext.Current.Response.Headers.Remove("Server");
// HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
// HttpContext.Current.Response.Headers.Remove("X-Powered-By");
//}
}
Thanks for your answers.
回答1:
First you can remove the server tags in your server ! and not let him place it again and again and you then remove it.
You can do that even on your web.config using the customHeaders
:
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
<remove name="X-UA-Compatible" />
<remove name="ETag" />
</customHeaders>
</httpProtocol>
The ETag or entity tag is way to flag a page and then see on your code if the page have been change and need to be update. If you see the ETag then some part of your code is added for this check and I think that you must left it as it is because you break this logic of the program.
If this ETag is places by server to flag images or similar items, you can avoid most of this tag by adding the static content be live more, and this is also can be done on iis, or on web.config as.
<staticContent>
<clientCache cacheControlMaxAge ="8.00:00:00" cacheControlMode="UseMaxAge" />
</staticContent>
So I think that the module that you made is not necessary for this thinks.
来源:https://stackoverflow.com/questions/11393649/configuring-etags-with-http-module-in-asp-net