简体   繁体   中英

Compression / Caching and Request Object not working together ASP.NET

I have a slight issue with an ASP.NET application,

I have configured a ViewBag variable to send to my View (using razor) the next page link with querystrings but when enabling this attribute :

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
    }
  }

The site doesn't take care of the following statement entirely :

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

it only produces the link : /culture/next/pageName but the querystring is not included (it's mark as null).

Is anything in my CompressAttribute that can cause that ? Because clearly when disabling it the redirection works.

EDIT :

It seems that the cache is in cause. Maybe the server doesn't re-render this link when reloading the page with different query.

The server returns the same cached page even when the query string has changed. To tell server to vary the cache per query string, use the HttpCacheVaryByParams .

Example:

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

By the way, you may want to use the OutputCacheAttribute and IIS Compression instead of rolling your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM