从静态资源中删除查询字符串

Remove query strings from static resources

我的网站是 运行 在 ASP.NET 平台上,最近我在 pingdom 上测试我的网站,我发现了以下错误。

Resources with a "?" in the URL are not cached by some proxy caching servers. Remove the query string and encode the parameters into the URL for the following resources: https://projectsdeal.co.uk/ScriptResource.axd?d ... 63Nawdr4rAt1lvT7c_zyBEkV9INg0&t=ffffffffe3663df5 https://projectsdeal.co.uk/ScriptResource.axd?d ... JGTlZFM0WRegQM9wdaZV3fQWMKwg2&t=ffffffffe3663df5

保持原样(这不是错误!)- 您不能从资源中删除此查询字符串,因为这是关于如何从中加载该资源的 ID asp.net

您收到的消息实际上是在谈论代理缓存服务器 - 什么是代理缓存服务器?缓存您网站页面的中间计算机,而不是实际的客户端计算机 - 可以在缓存中保存该页面并且通常不会降低您的网站速度。

因此,如果您正确设置它们,您的客户端可以将该资源保存在缓存中,据我所知,asp.net 正确处理并且您的资源被缓存得很好- 查看此屏幕截图。

现在,如果您想添加更积极的缓存,您可以使用 global.asax 并执行类似

的操作
protected void Application_BeginRequest(Object sender, EventArgs e)
{
    string cTheFile = HttpContext.Current.Request.Path;

    if (cTheFile.EndsWith("WebResource.axd", StringComparison.InvariantCultureIgnoreCase))
    {
        JustSetSomeCache(app);
    }
}

private static void JustSetSomeCache(HttpApplication app)
{
    app.Response.Cache.AppendCacheExtension("post-check=900, pre-check=3600");
    app.Response.Cache.SetExpires(DateTime.UtcNow.AddHours(32));
    app.Response.Cache.SetMaxAge(new TimeSpan(32, 0, 0));
    app.Response.Cache.SetCacheability(HttpCacheability.Public);
    app.Response.AppendHeader("Vary", "Accept-Encoding");
}

有什么不同?第二个缓存根本不像 asp.net 那样检查服务器是否有文件更改,你可以获得一个网络服务器调用。