I'm trying to implement caching in ASP.NET MVC 4 using the OutputCache attribute.
Here is my controller action:
[HttpGet]
[OutputCache(Duration = CACHE_DURATION, VaryByCustom = "$LanguageCode;myParam", Location = OutputCacheLocation.Server)]
public JsonResult MyAction(string myParam)
{
// this is called also if should be cached!
}
And here is the GetVaryByCustomString in the Global.asax:
public override string GetVaryByCustomString(HttpContext context, string arg)
{
var pars = arg.Split(';');
if (pars.Length == 0) return string.Empty;
var res = new System.Text.StringBuilder();
foreach (var s in pars)
{
switch (s)
{
case "$LanguageCode":
var culture = CultureManager.GetCurrentCulture();
res.Append(culture.Name);
break;
default:
var par = context.Request[s];
if (par != null)
res.AppendFormat(par);
break;
}
}
return base.GetVaryByCustomString(context, res.ToString());
}
This method is always called and returns the right value (e.g. "it123"
).
If I call the action with the only myParam
parameter, the cache works correctly.
http://localhost:1592/MyController/MyAction?myParam=123 // called multiple times always read from cache
The problem is that when I call the action with another parameter, not included in the VaryByCustom
string, the controller action is called anyway, also if is should be cached and the GetVaryByCustomString
returns the same result.
http://localhost:1592/MyController/MyAction?myParam=123&dummy=asdf // called multiple times with different 'dummy' values always calls the action
Any idea?