关闭的ETag没有服务器

问题描述:

你好,我 刚开始ASP.NET MVC 4和我在一个企业实习谁告诉我没有服务器创建一个基本网店,然后验证HTML和网站的速度与YSlow关闭的ETag没有服务器

我一直很忙,当我完成了网上商店,我开始使用YSlow的应用速度的网站,但有一件事我似乎无法修复,这是配置错误的ETag
“有5个具有错误配置ETags的组件“< - 这些是我的CSS文件和我用过的图片。 我一直在研究ETags是什么,仍然不完全知道他们做什么。

我知道Apache可以通过说FileETag none来关闭它们,但是在这种情况下,我没有使用服务器,我仍然想关闭它们,因为它们对分数不满意99.

我在找什么是一个答案什么ETags正好做,并解决我的问题。

感谢

+1

你在运行什么呢?如果它放在笔记本电脑上,他们会比yslow有更大的性能问题。 – dove

+0

你是什么意思?它只是在他们办公室里的一台电脑上,与性能无关,他们只是想看看我能否达到100 – Conceptual

+0

的整体表现分数啊好吧,这是一个练习,我会回答下面 – dove

按照以下,这是一个锻炼和显然是一个PC不会是非常高性能的意见。你可以使用httpHandler。这里有一个我使用的图像,将与您的YSlow的帮助(但请注意,这是没有的性能保障,是为指导,以非常繁忙的站点)

public class ImageHandler : IHttpHandler 
    { 
     public void ProcessRequest(HttpContext context) 
     { 
      context.Response.Cache.SetCacheability(HttpCacheability.Public); 
      context.Response.Cache.SetMaxAge(new TimeSpan(28, 0, 0, 0, 0)); 

      // Setting the last modified date to the creation data of the assembly 
     Assembly thisAssembly = Assembly.GetExecutingAssembly(); 
     string thisPath = thisAssembly.CodeBase.Replace(@"file:///", ""); 
     string assemblyName = "yourAssembly"; 
     string assemblyPath = thisPath.Replace(thisAssembly.ManifestModule.ScopeName, assemblyName); 

     var assemblyInfo = new FileInfo(assemblyPath); 
     var creationDate = assemblyInfo.CreationTime; 
     string eTag = GetFileETag(assemblyPath, creationDate);  
     // set cache info 
     context.Response.Cache.SetCacheability(HttpCacheability.Private); 
     context.Response.Cache.VaryByHeaders["If-Modified-Since"] = true; 
     context.Response.Cache.VaryByHeaders["If-None-Match"] = true; 
     context.Response.Cache.SetLastModified(creationDate); 
     context.Response.Cache.SetETag(eTag); 
if (IsFileModified(assemblyPath, creationDate, eTag, context.Request)) 
     { 
      //context.Response.ContentType = <specify content type>; 
      // Do resource processing here 
      context.Response.TransmitFile(context.Request.PhysicalPath); 
     } 
     else 
     { 
      // File hasn't changed, so return HTTP 304 without retrieving the data 
      context.Response.StatusCode = 304; 
      context.Response.StatusDescription = "Not Modified"; 

      // Explicitly set the Content-Length header so the client doesn't wait for 
      // content but keeps the connection open for other requests 
      context.Response.AddHeader("Content-Length", "0");   
     } 


     context.Response.End(); 


     } 


     public bool IsReusable 
     { 
      get { return false; } 
     } 


     /// <summary> 
     /// Checks if the resource assembly has been modified based on creation date. 
     /// </summary> 
     /// <remarks> 
     /// </remarks> 
     /// <seealso cref="GetFileETag"/> 
     private bool IsFileModified(string fileName, DateTime modifyDate, string eTag, HttpRequest request) 
     { 
      // Assume file has been modified unless we can determine otherwise 
      bool FileDateModified = true; 
      DateTime ModifiedSince; 
      TimeSpan ModifyDiff; 
      bool ETagChanged; 

      // Check If-Modified-Since request header, if it exists 
      string ifModifiedSince = request.Headers["If-Modified-Since"]; 
      if (!string.IsNullOrEmpty(ifModifiedSince) && ifModifiedSince.Length > 0 && DateTime.TryParse(ifModifiedSince, out ModifiedSince)) 
      { 
       FileDateModified = false; 
       if (modifyDate > ModifiedSince) 
       { 
        ModifyDiff = modifyDate - ModifiedSince; 
        // Ignore time difference of up to one seconds to compensate for date encoding 
        FileDateModified = ModifyDiff > TimeSpan.FromSeconds(1); 
       } 
      } 
      // Check the If-None-Match header, if it exists. This header is used by FireFox to validate entities based on the ETag response header 
      ETagChanged = false; 
      string ifNoneMatch = request.Headers["If-None-Match"]; 
      if (!string.IsNullOrEmpty(ifNoneMatch) && ifNoneMatch.Length > 0) 
      { 
       ETagChanged = ifNoneMatch != eTag; 
      } 
      return ETagChanged || FileDateModified; 
     } 

     /// <summary> 
     /// Generates a caching ETag based on file name and creation date. 
     /// </summary> 
     /// <remarks> 
     /// </remarks> 
     /// <seealso cref="GetFileETag"/> 
     private string GetFileETag(string fileName, DateTime modifyDate) 
     { 
      string fileString; 
      Encoder stringEncoder; 
      int byteCount; 
      Byte[] stringBytes; 

      // Use file name and modify date as the unique identifier 
      fileString = fileName + modifyDate.ToString(); 

      // Get string bytes 
      stringEncoder = Encoding.UTF8.GetEncoder(); 
      byteCount = stringEncoder.GetByteCount(fileString.ToCharArray(), 0, fileString.Length, true); 
      stringBytes = new Byte[byteCount]; 

      stringEncoder.GetBytes(fileString.ToCharArray(), 0, fileString.Length, stringBytes, 0, true); 

      //{ Hash string using MD5 and return the hex-encoded hash } 
      MD5 Enc = MD5CryptoServiceProvider.Create(); 
      return "\"" + BitConverter.ToString(Enc.ComputeHash(stringBytes)).Replace("-", string.Empty) + "\""; 

     } 
    } 
} 

,然后在你的配置指定处理程序(也可做在httphandlers下如果不使用iis7)

<add name="pngs" verb="*" path="*.png" type="yourAssembly.HttpHandlers.ImageHandler, hcs.web" preCondition="managedHandler" /> 
    <add name="jpgs" verb="*" path="*.jpg" type="yourAssembly.HttpHandlers.ImageHandler, hcs.web" preCondition="managedHandler" /> 
    <add name="gif" verb="*" path="*.gif" type="yourAssembly.HttpHandlers.ImageHandler, hcs.web" preCondition="managedHandler" /> 
+0

感谢您的建设性帮助,我想我必须将3个处理程序添加到system.webServer中?但如果我这样做,我会得到一个警告元素'system.webServer'具有无效的子元素'添加'。预期可能的要素列表:“在这里插入可能的要素列表,这是一个很长的列表”。 – Conceptual

+0

您必须在webServer节点和这些节点之间放置一个 dove

+0

哦好吧,谢谢,工作! – Conceptual