在asp.net中使用cookie设置文化,未更新

发布于 2024-10-08 14:25:20 字数 1061 浏览 2 评论 0原文

我正在使用 asp.net,并且希望用户可以自己设置要在网站中使用的文化。在 MasterPage 中,我有以下代码来设置语言 cookie:

protected void Page_Load(object sender, EventArgs e) {

    if (Request.QueryString["setLanguage"] != null)
    {
        HttpCookie languageCookie = new HttpCookie("language");
        languageCookie.Value = Request.QueryString["setLanguage"];
        languageCookie.Expires = DateTime.Now.AddDays(10);
        Response.SetCookie(languageCookie);
    }
}

在 Global.asax 中,我像这样使用 cookie:

protected void Application_BeginRequest(object sender, EventArgs e) {
    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["language"];
    if (languageCookie.Value != null)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);
    }
}

问题是,在使用 Response.SetCookie 设置 cookie 后,我需要重新加载页面以获取新语言。如何编写代码,以便当用户设置新语言时,页面会直接使用新语言重新加载?

I'm using asp.net and want to make it possible for the user to set the culture to use in the website by himself. In MasterPage I have the following code to set a language cookie:

protected void Page_Load(object sender, EventArgs e) {

    if (Request.QueryString["setLanguage"] != null)
    {
        HttpCookie languageCookie = new HttpCookie("language");
        languageCookie.Value = Request.QueryString["setLanguage"];
        languageCookie.Expires = DateTime.Now.AddDays(10);
        Response.SetCookie(languageCookie);
    }
}

In Global.asax I use the cookie like this:

protected void Application_BeginRequest(object sender, EventArgs e) {
    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["language"];
    if (languageCookie.Value != null)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);
    }
}

The problem is that after I set the cookie with Response.SetCookie I need to reload the page to get the new language. How can I make my code so when the user set a new language the page is reloaded with the new language directly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

贵在坚持 2024-10-15 14:25:20

你可以这样做

Response.Redirect(Request.PathAndQuery);

,但为什么不直接在设置 Cookie 后设置语言呢?您甚至可以使用 BeginRequest 事件来检查正在发布的特定输入,并将其用作设置语言的替代条件。

You can do

Response.Redirect(Request.PathAndQuery);

But why not just set the language after setting the Cookie? You can even use the BeginRequest event to check for specific input being posted and use it as an alternative condition for setting the language.

帝王念 2024-10-15 14:25:20

我对用户选择的语言也有同样的问题。为了让它工作,你必须

protected override void InitializeCulture()
{
    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["language"];

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);

}

在网站的每个页面上工作,我创建了一个从 System.Web.UI.Page 继承的类并在那里实现,

public class myBasePage : System.Web.UI.Page
{
  protected override void InitializeCulture()
  {
    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["language"];

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);

    base.InitializeCulture();
  }
}

从那时起我就拥有了我的所有页面从 myBasePage 继承。

这样,我使用服务器(回发)控件来设置语言,页面将被重新加载,并且语言将被设置。

I had the same issue with the language being selected by the user. In order for it to work you have to do it on

protected override void InitializeCulture()
{
    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["language"];

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);

}

In order for it to work on every page of the site, I created a class that inherited from System.Web.UI.Page and implemented there

public class myBasePage : System.Web.UI.Page
{
  protected override void InitializeCulture()
  {
    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["language"];

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);

    base.InitializeCulture();
  }
}

from then on I had all my pages inherit from myBasePage.

This way, I used a Server (Postback) control to set the language and the page would get reloaded, and the language would be set.

冷夜 2024-10-15 14:25:20

如果您使用的是 Asp.Net MVC

//A foreigner, has possibly brew a cookie for me
public class SpeakNativeTongueAttribute : ActionFilterAttribute, IActionFilter
{
    const string cookieName = "culture";

     void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
    {
        var cookieKeys = filterContext.RequestContext.HttpContext.Request.Cookies.AllKeys;

        if (cookieKeys.Contains(cookieName))
        {
            //eat the cookie
            var theCultureCookie = filterContext.RequestContext.HttpContext.Request.Cookies[cookieName];
            var theCulture = theCultureCookie.Value;

            //say thanks in native tongue
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(theCulture);
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(theCulture);
        }
        else
        {
            //Didn't receive a cookie, don't speak their language, those bastards!

        }
    }
}

If you are using Asp.Net MVC

//A foreigner, has possibly brew a cookie for me
public class SpeakNativeTongueAttribute : ActionFilterAttribute, IActionFilter
{
    const string cookieName = "culture";

     void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
    {
        var cookieKeys = filterContext.RequestContext.HttpContext.Request.Cookies.AllKeys;

        if (cookieKeys.Contains(cookieName))
        {
            //eat the cookie
            var theCultureCookie = filterContext.RequestContext.HttpContext.Request.Cookies[cookieName];
            var theCulture = theCultureCookie.Value;

            //say thanks in native tongue
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(theCulture);
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(theCulture);
        }
        else
        {
            //Didn't receive a cookie, don't speak their language, those bastards!

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