1

我有一个网站询问用户的位置,然后将其存储在 cookie 中。我想在网站上添加缓存,因此对相同 url/querystring/cookie 的任何请求都将首先使用客户端的浏览器缓存。varyByCustom我可以通过使用in myoutputCacheProfiles并为服务器上的每个请求创建一个自定义字符串来使这项工作用于服务器缓存。我找不到任何方法来强制用户在使用新位置更新 cookie 后请求每个页面的新版本,而不需要他们在浏览器上点击“刷新”。

如何包含 cookie 作为 Web 浏览器客户端缓存的依赖项?

这是我目前正在做的事情,它适用于在服务器上缓存不同的 url/querystring/cookie 组合。

在 web.config 中。

<system.web>
...
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="DefaultCacheProfile" enabled="true" duration="60"  varyByCustom="latlon" varyByParam="None" />
        </outputCacheProfiles>        
      </outputCacheSettings>
    </caching>

使用我页面上的个人资料...

<%@ OutputCache CacheProfile="DefaultCacheProfile" %>

在 global.asax.cs 中(CookieSupport是我读取我的 cookie 的内部类)

public override string GetVaryByCustomString(System.Web.HttpContext context, string custom)
{

    if (custom.ToLower() == "latlon") {
        if (CookieSupport.CookieExists) {
            double lon = double.Parse(CookieSupport.CookieValueGet("lon"));
            double lat = double.Parse(CookieSupport.CookieValueGet("lat"));

            string varystring = string.Format("{0},{1},{2}", Request.QueryString, lat, lon);

            return varystring;
        } else {
            return Request.QueryString.ToString;
        }
    }

    return base.GetVaryByCustomString(context, custom);
}

以下是我创建 cookie 的方法...

System.Web.HttpCookie oCookie = new System.Web.HttpCookie(CookieSupport.CookieName);
oCookie.Values.Add("lat", data_Lat.ToString);
oCookie.Values.Add("lon", data_Lng.ToString);    
Response.Cookies.Add(oCookie);
4

0 回答 0