压缩/缓存和请求对象不能一起使用ASP.NET



我有一个ASP.NET应用程序的小问题

我已经配置了一个ViewBag变量,将带有查询字符串的下一页链接发送到我的View(使用剃刀),但当启用此属性时:

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,但不包括查询字符串(标记为null)。

我的CompressAttribute中有什么东西会导致这种情况吗?因为很明显,当禁用它时,重定向是有效的。

编辑:

看来缓存是有原因的。也许服务器在使用不同查询重新加载页面时不会重新呈现此链接。

即使查询字符串发生更改,服务器也会返回相同的缓存页面。若要告诉服务器按查询字符串更改缓存,请使用HttpCacheVaryByParams。

示例:

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

顺便说一句,您可能希望使用OutputCacheAttribute和IIS压缩,而不是滚动自己的。

最新更新