2014-06-29 33 views
1

我已經寫了一個簡單的IHttpModule添加ETag的使用IHttpModule的

void context_PreSendRequestHeaders(object sender, EventArgs e) 
{ 
    //remove default 
    HttpContext.Current.Response.Headers.Remove("ETag"); 

    //add version one 
    HttpContext.Current.Response.Headers.Add("ETag", "Test1.0"); 
} 

,我想刪除IIS的ETag並添加自己的來自客戶端的控制JavaScript和CSS文件的請求 - 在更新的情況下,我想它會自動刷新。 客戶端的響應確定ETag的

如果 - 無 - 匹配:Test1.0 如果-Modified-Since的:星期一,2014年6月2日11時08分54秒GMT

但IIS總是返回內容,而不是304

+0

可能重複[如何從IIS7中刪除ETag頭?](http://stackoverflow.com/questions/477913/how-do-i-remove-etag- headers-from-iis7) –

+0

這個問題並沒有刪除ETag,因爲我刪除了它,加我自己的。但是當我添加自己的IIS時,將它視爲新請求,並且從不發送304 –

回答

0
void context_PreSendRequestHeaders(object sender, EventArgs e) 
    { 
     var etag = "Test4-0"; 

     //remove default 
     HttpContext.Current.Response.Headers.Remove("ETag"); 

     //add version one 
     HttpContext.Current.Response.Headers.Add("ETag", etag); 

     string ifNoneMatch = HttpContext.Current.Request.Headers["If-None-Match"]; 
     Debug.WriteLine(String.Format("ifNoneMatch - {0}", ifNoneMatch)); 

     if (ifNoneMatch != null && ifNoneMatch.Contains(",")) 
     { 
      ifNoneMatch = ifNoneMatch.Substring(0, ifNoneMatch.IndexOf(",", StringComparison.Ordinal)); 
     } 

     HttpContext.Current.Response.Cache.VaryByHeaders["If-None-Match"] = true; 
     Debug.WriteLine(String.Format("ifNoneMatch - etag: {0}-{1}", ifNoneMatch, etag)); 
     if (etag == ifNoneMatch) 
     { 
      Debug.WriteLine(String.Format("ifNoneMatch2 - etag: {0}-{1}", ifNoneMatch, etag)); 
      HttpContext.Current.Response.ClearContent(); 
      HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotModified; 
      HttpContext.Current.Response.SuppressContent = true; 
     } 
    } 
}