0

I'm trying to use the OutputCache attribute to cache pages depending on the language users selected.

[OutputCache(Duration = 86400, Location = OutputCacheLocation.Client, VaryByParam = "", VaryByCustom = "lang")]
public ActionResult MyActionMethod()
{
    ...
}

It works fine when we are on the page and we change the language, cool!

But the thing is: when a user calls the page for the first time, there is no "lang" parameter. So the cache will be created without parameter and it won't be replace if we change the language after.

How can I manage this case, when there is no parameter?

Any help would be appreciated, thanks!

Léo Davesne
  • 2,103
  • 1
  • 21
  • 24

1 Answers1

0

You are talking about there is not "lang" parameter, you mean, there is no "lang" custom?

In global.asax you should have something like this:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom == "lang")
    {
        string lang = null;

        if (Request.UserLanguages != null && Request.UserLanguages.Length > 0)
        {
            lang = Request.UserLanguages.First().Split(new char[] { ';' }).First();
        }
        else
        {
            // Default
            lang = "en-US";
        }

        return string.Format("lang={0}", lang.ToLower());
    }

    return base.GetVaryByCustomString(context, custom);
}

Then it will have the value "en-US" as default and otherwise get it from the browser in this case, or implement it using cookie.

KoalaBear
  • 2,755
  • 2
  • 25
  • 29