避免让这种文化检查每次通话?
让我向您介绍我的场景的一些背景信息。我有一个多语言网站,我的文化存储在数据库中,并获得了一个属性 public bool Active { get;放; }
如果您访问我的网站时浏览器上有俄罗斯文化,那么我的网站不支持这种文化,因此我需要将文化设置为“se”(或其他)。
如果您以受支持的文化访问我的网站,但其不处于活动状态,我需要将其设置为默认的“se”(或其他)。
现在我可以在我的重写 IHttpHandler GetHttpHandler
方法中轻松完成此检查。问题是我不想每次网站在网站上进行任何调用时都调用数据库。我的想法是我做一个会话[“firstVisit”]来减少检查,但我有点不知道应该如何去做,因为我的场景是这样的 未在 session["firstVisit"] 行上设置为对象的实例
,所以我的问题是如何处理这个问题?我还有什么其他选择可以解决这个问题?
我的想法是这样的
protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
{
String culture = requestContext.RouteData.Values["culture"].ToString();
if (HttpContext.Current.Session["firstVisit"].ToString() == string.Empty)
{
//do the check
}
var ci = new CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
return base.GetHttpHandler(requestContext);
}
编辑
为什么我没有想到这一点,我最终只是在protected void Application_Start()
中进行了检查以避免会话
Let me give you some background on my scenario. I got a multi language site and my cultures are stored in database and got a property that is public bool Active { get; set; }
If you come to my site with lets say a Russian culture on your browser, that is not supported on my site so i need to set the culture to "se" (or whatever).
If you come to my site with a supported culture but its not Active i need to set it to a default one "se" (or whatever).
Now I can do this check easy in my override IHttpHandler GetHttpHandler
method. The thing is I dont want to make the call to the database everytime the site makes any call on the site. My thought is that I do a session["firstVisit"] to reduce that check but I kinda don't know how i should go about to that, because my scenario it say thatnot set to an instance of an object
on the session["firstVisit"] line, so my question is how do I handle this? And what other options I got to go about this?
my thought is something like this
protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
{
String culture = requestContext.RouteData.Values["culture"].ToString();
if (HttpContext.Current.Session["firstVisit"].ToString() == string.Empty)
{
//do the check
}
var ci = new CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
return base.GetHttpHandler(requestContext);
}
EDIT
Doh why did I not think of that, I ended up just doing a check in the protected void Application_Start()
to avoid sessions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用global.asax.cs:
首次创建会话时调用此方法。然后,您可以进行检查,将您的语言信息填充到会话中,并在会话通过用户与您的网站交互的过程中保持活动状态。
Use global.asax.cs:
This method is called when the session is first created. You can do the check then, stuff your language info in the session and have it for as long as the session is live through the user's interaction with your site.