本地化时不同语言选择时的缓存

本地化时缓存是个问题,只有通过outputcache 的varybycustom来实现用户选择不同的语言 时的缓存。但是.net 是不支持session做为varybycustom的参数只能用cookie.

1.用户选择语言时用一个cookie记下所选。
        HttpCookie mycookie = new HttpCookie("favoritelang");
        mycookie.Value = "en-US";
        mycookie.Expires.AddYears(1);
        Response.Cookies.Add(mycookie);

2.Global.asax里创建:
  public override string GetVaryByCustomString(HttpContext context, string arg)
    {
        if(arg.ToLower() == "favoritelang")
  {
      HttpCookie cookie = context.Request.Cookies["favoritelang"];
    if(cookie != null)
      return cookie.Value;
  }
  return base.GetVaryByCustomString(context, arg);
}

3.页面文件中加入
<%@ OutputCache Duration="1000" VaryByParam="none" VaryByCustom="favoritelang" %>

that's it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值