Asp.Net MVC在不手动清除缓存的情况下更改资源值

时间:2016-12-30 08:58:08

标签: c# asp.net-mvc multilingual

在我的Asp.Net MVC应用程序中,用户来自另一个系统,我将在下面记录它们。

IAccount account = null;

if (userType == UserType.SystemUser)
    account = _accountService.CheckSystemUser(userId);
if (userType == UserType.Employee)
    account = _accountService.CheckEmployee(userId);

if (account == null) throw new Exception(ErrorMessages.UserNotFound);

var roles = account.Roles.ToArray();

var principalModel = new CustomPrincipalViewModel
{
    UserId = account.UserId.ToString(),
    FullName = account.FullName,
    Roles = roles,
    Language = account.Language
};

var userData = JsonConvert.SerializeObject(principalModel);
var ticket = new FormsAuthenticationTicket(1, principalModel.FullName, DateTime.Now, DateTime.Now.AddMinutes(30), false, userData);
var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
Response.Cookies.Add(cookie);

SetCulture(account.Language.CultureCode);

return Json(new { isSuccess = true, userFullName = account.FullName });

使用SetCulture方法设置当前文化。

private void SetCulture(string culture)
{
    culture = CultureHelper.GetImplementedCulture(culture);
    var cookie = Request.Cookies["_culture"];
    if (cookie != null)
    {
        cookie.Value = culture;
        cookie.Expires = DateTime.Now.AddYears(1);
    }
    else
    {
        cookie = new HttpCookie("_culture")
        {
            Value = culture,
            Expires = DateTime.Now.AddYears(1)
        };
    }
    Response.Cookies.Add(cookie);
}

我的观看页面我从资源文件中设置文本值,如下所示。

<span class="pageButton"> @Global.Cancel </span>

但是当我从其他系统更改用户语言并再次登录到我的系统时,所有文本仍然使用以前的语言。用ctrl + F5刷新页面后,所有内容都以真实语言显示。

有没有办法强制在没有手动清除缓存的情况下以新语言加载页面文本?

1 个答案:

答案 0 :(得分:0)

Cookie var cookie = Request.Cookies["_culture"];中的问题,仍然保留旧值,在logOff或尝试再次登录时清除它

您还可以使用Thread.CurrentThread来设置文化

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