ASP.Net MVC switching Cultures after compile for initial load

廉价感情. 提交于 2019-12-10 19:04:05

问题


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).


回答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.




回答2:


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.




回答3:


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




回答4:


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>" %>



回答5:


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);
    }
}


来源:https://stackoverflow.com/questions/4091602/asp-net-mvc-switching-cultures-after-compile-for-initial-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!