2013-11-28 31 views
0

我有一個ASP.NET應用程序中的輕微的問題,壓縮/緩存和請求對象不是一起工作ASP.NET

我配置了一個ViewBag變量發送到我的視圖(用剃刀)的下一個頁面鏈接與查詢字符串,但啓用此屬性時:

public class CompressAttribute : System.Web.Mvc.ActionFilterAttribute 
    { 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     #region Cache 
     HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddDays(1)); 
     HttpContext.Current.Response.Cache.SetValidUntilExpires(true); 
     HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public); 
     HttpContext.Current.Response.Cache.VaryByHeaders["Accept-Encoding"] = true; 
     #endregion 
     #region Compression 
     var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"]; 
     if (string.IsNullOrEmpty(encodingsAccepted)) return; 

     encodingsAccepted = encodingsAccepted.ToLowerInvariant(); 
     var response = filterContext.HttpContext.Response; 

     if (encodingsAccepted.Contains("deflate")) 
     { 
     response.AppendHeader("Content-Encoding", "deflate"); 
     response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); 
     } 
     else if (encodingsAccepted.Contains("gzip")) 
     { 
     response.AppendHeader("Content-Encoding", "gzip"); 
     response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); 
     } 
     #endregion 
    } 
    } 

該網站不小心下面的語句完全:

ViewBag.NextPageLink = "/" + culture + "/next/" + pageName + Request.Url.Query; 

它只是生產環節:/culture/next/pageName,但不包含查詢字符串(它標記爲空)。

在我的CompressAttribute中有什麼可以導致這種情況?因爲很明顯,禁用重定向時會起作用。

編輯:

看來這個緩存是原因。當用不同的查詢重新加載頁面時,服務器可能不會重新呈現此鏈接。

回答

0

即使查詢字符串已更改,服務器也會返回相同的緩存頁面。 要告訴服務器更改每個查詢字符串的緩存,請使用HttpCacheVaryByParams

例子:

HttpContext.Current.Response.Cache.VaryByParams["*"] = true; //* means all params 

順便說一句,你可能想使用OutputCacheAttribute和IIS壓縮,而不是滾動您自己的。

+0

所有的參數意味着在URL中設置的每個參數?或查詢字符串的每個鍵?因爲我唯一需要的是重新加載查詢字符串:) – Revv

+0

所有HTTP Get或Post參數ASP.NET。你可以通過使用這種格式告訴ASP.net只改變特定的參數:Response.Cache.VaryByParams [「Category」] = true; // Category param – LostInComputer

+0

前幾天我解決了我的問題^^。我用它作爲創建鏈接的函數的屬性。謝謝 – Revv