开发者

Cache-Control HTTP header does work properly

I am having issue with cache-control. I have one IIS website with multiple host headers. When you browse site number 1 then cache will be set for this site, when you open browser again and go to 2nd site you will see content from first site. How can I determine cache content based on the site user visits? Everything working fine when you have 1 site and host header related to SAME site.

//Set Cacheability
if (!Context.User.Identity.IsAuthenticated && _activeNode.CacheDuration > 0)
{
    var eTag = GetETag(_activeNode);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
    if (IsClientCached(_activeNode.UpdateTimestamp))
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.StatusCode =开发者_StackOverflow (int)HttpStatusCode.NotModified;
        HttpContext.Current.Response.SuppressContent = true;
        HttpContext.Current.Response.End();
        return;
    }

    var incomingEtag = HttpContext.Current.Request.Headers["If-None-Match"];
    if (String.Compare(incomingEtag, eTag) == 0)
    {
        HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotModified;
        HttpContext.Current.Response.SuppressContent = true;
        HttpContext.Current.Response.End();
        return;
    }

    HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.ToUniversalTime().AddMinutes(_activeNode.CacheDuration));
    HttpContext.Current.Response.Cache.SetMaxAge(new TimeSpan(0, _activeNode.CacheDuration, 0));
    HttpContext.Current.Response.Cache.SetLastModified(_activeNode.UpdateTimestamp);
    HttpContext.Current.Response.Cache.SetETag(eTag);
}

/// <summary>
/// Gets the ETag.
/// </summary>
/// <param name="node">The node.</param>
/// <returns></returns>
private static string GetETag(Node node)
{
    var etag = String.Format("{0}_{1}", node.Site.Id, node.Id);
    return "\"" + Encryption.StringToMD5Hash(etag).Replace("-", null) + "\"";
}

/// <summary>
/// Determines whether [is client cached] [the specified content modified].
/// </summary>
/// <param name="contentModified">The content modified.</param>
/// <returns>
///     <c>true</c> if [is client cached] [the specified content modified]; otherwise, <c>false</c>.
/// </returns>
private bool IsClientCached(DateTime contentModified)
{
    var header = Request.Headers["If-Modified-Since"];
    if (header != null)
    {
        DateTime isModifiedSince;
        if (DateTime.TryParse(header, out isModifiedSince))
        {
            return isModifiedSince > contentModified;
        }
    }

    return false;
}


Sounds like you should add the host header value to your IsClientCached algorithm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜