简体   繁体   English

编译初始加载后ASP.Net MVC切换文化

[英]ASP.Net MVC switching Cultures after compile for initial load

I have a hybrid ASP.Net web forms/MVC app. 我有一个混合ASP.Net Web表单/ MVC应用程序。 On one of the MVC "pages"/views, I have it render a bunch of dates using the ToShortDateString() and ToLongDateString(). 在其中一个MVC“页面”/视图中,我使用ToShortDateString()和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. 99%的时间它是en-US,但在编译后第一次加载MVC视图时,它被设置为en-GB。 If I reload the page immediately after that, it's back to en-US. 如果我在此之后立即重新加载页面,它将返回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. 我已经尝试将web.config文件中的culture和uiculture设置为en-US以强制它正确,但没有运气。

Anyone have any ideas on this one? 有人对这个有任何想法吗? Bug in MVC? 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 并将其包含在web.config中

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

and this in the header of the .aspx file 这在.aspx文件的标题中

<%@ 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. 其他Web表单页面不受影响,即使它们来自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. 只是更改.aspx文件不会导致这种情况,必须编译c#代码才能导致这种情况。

More data: I have it tracked down to the Render method. 更多数据:我将其跟踪到Render方法。 Before the Render method, the culture is en-US and afterwards it is en-GB (again only on initial pageload after compilation). 在Render方法之前,文化是en-US,之后它是en-GB(再次仅在编译后的初始页面加载)。

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)? 在您的视图中,尝试创建一个基本视图 - 然后对于该特定视图继承它,如下所示: 如何全球化ASP.NET MVC视图(特别是小数分隔符)? 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. 我认为全球化密钥正常运作存在一些问题。

This turned out to be caused by an a dependency on an out of date, third party .dll. 原来这是由对过时的第三方.dll的依赖造成的。 Once I tracked it down, and got the updated .dll, all was good again. 一旦我追踪它,并得到更新的.dll,一切都很好。

how have you set the culture into the web config ? 你如何将文化设置为Web配置?

are you using the "globalization" key ? 你在使用“全球化”键吗?

have a look at: 看一下:

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

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

My first observation for your .aspx page, 我对.aspx页面的第一次观察,

You are not inheriting your ASPX page from base ViewPage class. 您没有从基础ViewPage类继承您的ASPX页面。 Try adding this to your ASPX page header tag. 尝试将此添加到您的ASPX页眉标记。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM