本地化时缓存是个问题,只有通过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.
本文介绍如何在ASP.NET中使用输出缓存实现多语言支持。通过设置cookie记录用户选择的语言,并在Global.asax中定义GetVaryByCustomString方法来实现根据不同语言变化的缓存策略。
1614

被折叠的 条评论
为什么被折叠?



