在asp.net中使用cookie设置文化,未更新
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做
,但为什么不直接在设置 Cookie 后设置语言呢?您甚至可以使用 BeginRequest 事件来检查正在发布的特定输入,并将其用作设置语言的替代条件。
You can do
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.
我对用户选择的语言也有同样的问题。为了让它工作,你必须
在网站的每个页面上工作,我创建了一个从 System.Web.UI.Page 继承的类并在那里实现,
从那时起我就拥有了我的所有页面从 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
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
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.
如果您使用的是 Asp.Net MVC
If you are using Asp.Net MVC