操作方法 URL HTTP 压缩和使用 Web.config 的缓存



我已经购买了 microhosting.in 的共享主机 在为我的创业公司建立网站后,自然的下一步是推广。所以我检查了性能。Pagespeed测试指出缓存和未压缩的http是麻烦。现在,通过Plesk托管Windows意味着对于任何IIS功能要求,我必须明确说服托管提供商,除非使用Web.config就可以了。技术团队在尝试诸如 http://blog.fi.net.au/?p=372 之类的事情方面有点弱 使用 http 启用公共缓存,因为代理缓存可能会避免元标记内容嗅探,应该是可取的。是否有任何自定义实现可以与 Web.config 结合使用,以应用这些实现,因为我在任何地方都可以使用 .htaccess 找到解决方案?

使用自己的 web.config 将可选内容压缩在单独的文件夹中,该 web.config 强制将与内容编码相关的属性添加到标头。对于必需的内容,重命名文件以在运行时使用 asp.net 并放置 -->将 ContentType 替换为预期的 MIME 类型,嵌套在某个标记中以将其放置在最外层或未注释作为默认在服务器端 xsl 转换期间恼火。现在在 global.asax 中实现以下 C# 逻辑

 void Application_PreRequestHandlerExecute(object sender, EventArgs e)
 {
     HttpApplication app = sender as HttpApplication;
    string acceptEncoding = app.Request.Headers["Accept-Encoding"];
    Stream prevUncompressedStream = app.Response.Filter;
    if (acceptEncoding == null || acceptEncoding.Length == 0)
        return;
    acceptEncoding = acceptEncoding.ToLower();
    if (acceptEncoding.Contains("gzip"))
    {
        // gzip
        app.Response.Filter = new GZipStream(prevUncompressedStream,
            CompressionMode.Compress);
        app.Response.AppendHeader("Content-Encoding", "gzip");
    }       /*else if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
    {
        // defalte
        app.Response.Filter = new DeflateStream(prevUncompressedStream,
            CompressionMode.Compress);
        app.Response.AppendHeader("Content-Encoding", "deflate");
    }*/    // Only cache for X seconds.
Response.Cache.SetExpires(DateTime.Now.AddMonths(9));
Response.Cache.SetMaxAge(new TimeSpan(270, 0, 0,0,0));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
// Sliding expiration means we reset the X seconds after each request.
// SetETag is required to work around one aspect of sliding expiration.
Response.Cache.SetSlidingExpiration(true);
Response.Cache.SetETagFromFileDependencies();
}

最新更新