2013-03-06 37 views
1

我有一个关于在任一ASP.Net或复合C1 CMS的输出缓存的工作(不知道哪来的问题)以下问题:重写GetVaryByCustomString方法被调用仅在第一次请求一个页面

我需要改变通过名为“城市”的cookie进行缓存。在Global.asax中,我重写了GetVaryByCustomString方法。

public override string GetVaryByCustomString(HttpContext context, string custom) 
{ 
    var result = ApplicationLevelEventHandlers.GetVaryByCustomString(context, custom); 

    if (result == null) { 
     return base.GetVaryByCustomString(context, custom); 
    } 

    HttpCookie cookie = context.Request.Cookies["City"]; 
    string additionalVary = ""; 
    if (cookie != null) 
     additionalVary = cookie.Value; 
    else 
     additionalVary = "Москва"; 

    return String.Concat(result, additionalVary); 
} 

而问题是:我重写GetVaryByCustomString方法的断点被击中仅在第一次请求一个页面,但旁边的请求没有被打,直到缓存已经到期(60秒在我案件)。 虽然cookie可能在60秒内在客户端发生了变化 - 该页面仅从缓存中获取。

我在哪里错了? 在此先感谢。

UPDATE: 我终于能够使输出缓存由饼干改变由以下修改的CacheProfile设置:

<outputCacheProfiles> 
    <add name="C1Page" duration="60" varyByCustom="C1Page" varyByParam="*" varyByHeader="Cookie" location="Server" /> 
</outputCacheProfiles> 

在这种情况下,方法GetVaryByCustomString被打在每个请求,但有不需要它:这些设置使缓存因网站上可能设置的每个cookie而异。当然,这不是一个理想的行为 - 这就是为什么我继续调查。

更有趣的是 - 这两个参数(varyByHeader =“Cookie”和位置=“服务器”)只能一起工作,禁用其中一个使事情看起来和以前完全一样 - outputcache没有变化。

回答

0

试着把一个断点放到Application_BeginRequest,它可能是浏览器没有提出请求,而是显示一个缓存页面。

+0

在EndRequest中试过 - 每个请求都会触发断点。 – user2138889 2013-03-07 10:53:49

+0

查看问题中的更新。 – user2138889 2013-03-07 11:47:11

相关问题