The GetVaryByCustomString method is not fired

758 views Asked by At

The GetVaryByCustomString method is not fired when I place a breakpoint in it.

The action I want to cache depending on the language the user selected:

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

The cache profile in the Web.config:

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

And in the Global.asax.cs file:

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

    return base.GetVaryByCustomString(context, value);
}

Any help would be appreciated, thanks!

1

There are 1 answers

0
Léo Davesne On

Actually location must be set to "Server", and not "Client".

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

And it works, phew!