避免让这种文化检查每次通话?

发布于 2024-10-16 00:06:45 字数 1276 浏览 0 评论 0原文

让我向您介绍我的场景的一些背景信息。我有一个多语言网站,我的文化存储在数据库中,并获得了一个属性 public bool Active { get;放; }

  1. 如果您访问我的网站时浏览器上有俄罗斯文化,那么我的网站不支持这种文化,因此我需要将文化设置为“se”(或其他)。

  2. 如果您以受支持的文化访问我的网站,但其不处于活动状态,我需要将其设置为默认的“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; }

  1. 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).

  2. 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 that
not 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2024-10-23 00:06:45

使用global.asax.cs:

protected void Session_Start( object sender, EventArgs e )

首次创建会话时调用此方法。然后,您可以进行检查,将您的语言信息填充到会话中,并在会话通过用户与您的网站交互的过程中保持活动状态。

Use global.asax.cs:

protected void Session_Start( object sender, EventArgs e )

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文