开发者

Cache based on Url Parameter in Asp.net MVC

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 b开发者_Python百科ased on Culture?


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜