3

i have defined a route culture/Controller/action/id... my controller contains following action..

 [OutputCache(Duration=60*10)]
        public ActionResult Index()
        {*/do magic here/*}

is it possible to Cache contents based on Culture?

1 Answer 1

4

The localization complete guide presents an example of how to achieve this using the VaryByCustom parameter. In global.asax you would override the GetVaryByCustomString method:

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

and then:

[OutputCache(Duration = 60 * 10, VaryByParam = "none", VaryByCustom = "lang")]
public ActionResult Index()
{
    /* do magic here */
    ...
}

Or if you want to rely solely on the culture route data parameter you could do this:

public override string GetVaryByCustomString(HttpContext context, string value)
{
    if (value == "lang")
    {
        var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(context));
        var culture = (string)routeData.Values["culture"];
        if (!string.IsNullOrEmpty(culture))
        {
            return culture;
        }
    }
    return base.GetVaryByCustomString(context, value);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.