本地化时缓存是个问题,只有通过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.