【问题标题】:The GetVaryByCustomString method is not fired未触发 GetVaryByCustomString 方法
【发布时间】:2015-01-31 09:06:47
【问题描述】:

当我在 GetVaryByCustomString 方法中放置断点时,它不会被触发。

我要缓存的操作取决于用户选择的语言:

[OutputCache(CacheProfile = "24HoursCache", VaryByCustom = "lang")]
public ActionResult MyAction()
{
    ...
}

Web.config 中的缓存配置文件:

<configuration>
    ...
    <system.web>
        ...
        <caching>
            <outputCacheSettings>
                <outputCacheProfiles>
                    <clear />
                    <add name="24HoursCache" duration="86400" location="Client" varyByParam="none" />
                </outputCacheProfiles>
            </outputCacheSettings>
        </caching>
    </system.web>
    ...
</configuration>

在 Global.asax.cs 文件中:

public override string GetVaryByCustomString(HttpContext context, string value)
{
    if (value.ToLower() == "lang")
    {
        return Thread.CurrentThread.CurrentUICulture.Name;
    }

    return base.GetVaryByCustomString(context, value);
}

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: c# asp.net-mvc outputcache varybyparam


    【解决方案1】:

    实际上位置必须设置为“服务器”,而不是“客户端”。

    <configuration>
            ...
            <system.web>
                ...
                <caching>
                    <outputCacheSettings>
                        <outputCacheProfiles>
                            <clear />
                            <add name="24HoursCache" duration="86400" location="Server" varyByParam="none" />
                        </outputCacheProfiles>
                    </outputCacheSettings>
                </caching>
            </system.web>
            ...
        </configuration>
    

    它有效,呸!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 2019-04-08
      • 2012-08-25
      • 2021-04-18
      • 2012-07-26
      相关资源
      最近更新 更多