I'm trying to change the user's current culture via AJAX POST:
$.ajax({
type: 'POST',
url: '/Account/SetDefaultCulture',
data: data,
dataType: 'JSON',
success: function (result) {
location.reload(true);
}
});
With this controller method:
private readonly AspEntities db = new AspEntities();
[HttpPost]
public JsonResult SetDefaultCulture(string userName, string culture)
{
if (!userName.IsNullOrEmpty())
{
var user = (from a in _db.AspNetUsers
where a.UserName == userName
select a).FirstOrDefault();
if (user != null)
{
user.Culture = culture;
db.SaveChanges();
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
return Json(new { culture, success = true });
}
}
return Json(new {culture, success = false});
}
If I step through while debugging, I can see the database record update right after db.SaveChanges()
is called.
The problem is the application/website doesn't update the user's Culture until AFTER I change some code in the Controller, and then build the project again. It's almost as if the Culture is being cached within the application or something.
I must be missing something. Is this common? Has anyone encountered this?
EDIT
I am using a Utility method to check the current user's culture:
public static CultureInfo GetCulture()
{
var userId = HttpContext.Current.User.Identity.GetUserId();
var user = (from a in _db.AspNetUsers
where a.Id == userId
select a).FirstOrDefault();
var culture = "en-CA";
if (user != null)
{
if (!user.Culture.IsNullOrEmpty())
{
culture = user.Culture;
}
}
return new CultureInfo(culture);
}
Within a Filter which runs on every page hit:
public class CheckCultureAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower().Trim();
string actionName = filterContext.ActionDescriptor.ActionName.ToLower().Trim();
//Set Culture
var culture = (ctx.User != null) ? Utilities.GetCulture() : new CultureInfo("en-CA");
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = culture;
...
}
}
If I start the debugger and step through this filter, the GetCulture()
method returns the non-updated value (which does not match the value that has been updated in the Database)