3

I have a hybrid ASP.Net web forms/MVC app. On one of the MVC "pages"/views, I have it render a bunch of dates using the ToShortDateString() and ToLongDateString(). These work correctly most of the time, but the first time I load the view after compiling the app, they are formatted incorrectly.

I traced this down and checked the current thread's culture. For 99% of the time it's en-US, but on the first load of the MVC view after compiling it is set to en-GB. If I reload the page immediately after that, it's back to en-US.

I have tried setting the culture and uiculture in the web.config file to en-US to force it to be correct, but no luck.

Anyone have any ideas on this one? Bug in MVC?

Edit (additional code and attempts): Even if I go completely overboard and include this in the base class of the view

public class DNViewPage<T> : ViewPage<T> where T : class
    {

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
            CultureInfo cultureInfo = new CultureInfo("en-US");
            this.Culture = "en-US";
            this.UICulture = "en-US";
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
            Thread.CurrentThread.CurrentCulture = cultureInfo;
        }

        protected void Page_Load(object sender, EventArgs e) {
            CultureInfo cultureInfo = new CultureInfo("en-US");
            this.Culture = "en-US";
            this.UICulture = "en-US";
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
            Thread.CurrentThread.CurrentCulture = cultureInfo;
        }

        protected override void InitializeCulture() {

            CultureInfo cultureInfo = new CultureInfo("en-US");
            this.Culture = "en-US";
            this.UICulture = "en-US";
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
            Thread.CurrentThread.CurrentCulture = cultureInfo;

            base.InitializeCulture();
        }
    }

and include this in the web.config

<globalization requestEncoding="utf-8" responseEncoding="utf-8" uiCulture="en-US" culture="en-US"/>

and this in the header of the .aspx file

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Culture="en-US" UICulture="en-US"

Again, this is only on the initial load after compiling the code, when that page first loads. Other web forms pages are unaffected, even if they descend from System.Web.Mvc.ViewPage. All subsequent loads treat the culture correctly. Just changing the .aspx file doesn't cause this, the c# code has to be compiled to cause this.

More data: I have it tracked down to the Render method. Before the Render method, the culture is en-US and afterwards it is en-GB (again only on initial pageload after compilation).

tereško
  • 58,060
  • 25
  • 98
  • 150
Jared
  • 7,165
  • 6
  • 49
  • 52

5 Answers5

1

In your view, try creating a base view - then for that particular view inherit from it as done here: How to globalize ASP.NET MVC views (decimal separators in particular)? however yours would be more like:

protected override void InitializeCulture()
{
    base.InitializeCulture();
    CultureInfo cultureInfo = new CultureInfo("en-US");
    Thread.CurrentThread.CurrentUICulture = cultureInfo;
}

I believe there have been some issues with the globalization key working correctly.

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • Great idea, but no luck. I've added more code samples above to show what I did and that it still didn't work. – Jared May 15 '11 at 18:38
  • Your code is different though than using initializeculture..... Did you specifically try that override? – Adam Tuliper May 15 '11 at 20:38
  • +1 I tried it in this order and no go. Actually gave me an idea though and I have it tracked down to the Render method. Before the Render method, the culture is en-US and afterwards it is en-GB (again only on initial pageload after compilation). – Jared May 16 '11 at 22:10
  • Ok, it turned out to be an external dependency. Once I got it narrowed down to the Render method --don't know why I didn't think of that before--I was able to track it down and update the 3rd party .dll. Points to you for that. – Jared May 16 '11 at 23:03
1

This turned out to be caused by an a dependency on an out of date, third party .dll. Once I tracked it down, and got the updated .dll, all was good again.

Jared
  • 7,165
  • 6
  • 49
  • 52
0

how have you set the culture into the web config ?

are you using the "globalization" key ?

have a look at:

http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • 1
    Yes, I tried setting the culture at the web.config, page and code level. Still no love. Like I said, it works fine after the initial load of the view, but that first load is off. – Jared Nov 04 '10 at 15:39
0

Have you tried making a base controller? I've changed culture with an app at work at it worked pretty well.

public class BaseController : Controller
{
    public string ActionName;
    public string ControllerName;

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //Switch the language in here?
       CultureInfo cultureInfo = new CultureInfo("en-US");
        this.Culture = "en-US";
        this.UICulture = "en-US";
        Thread.CurrentThread.CurrentUICulture = cultureInfo;
        Thread.CurrentThread.CurrentCulture = cultureInfo;

        base.OnActionExecuting(context);
    }
}
Lareau
  • 1,982
  • 1
  • 26
  • 47
0

My first observation for your .aspx page,

You are not inheriting your ASPX page from base ViewPage class. Try adding this to your ASPX page header tag.

Inherits="XXX.Views.DNViewPage<YYY.Models.zzzViewModel>"

so it should look like this,

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="XXX.Views.DNViewPage<YYY.Models.zzzViewModel>" %>
Priyank
  • 10,503
  • 2
  • 27
  • 25
  • Yea, that's in there. Again, this works great on subsequent page loads. Just not that first one after compilation. – Jared May 16 '11 at 22:02