ASP.Net MVC 在编译初始加载后切换区域性

发布于 2024-09-30 12:10:50 字数 2127 浏览 6 评论 0原文

我有一个混合 ASP.Net Web 表单/MVC 应用程序。在其中一个 MVC“页面”/视图上,我让它使用 ToShortDateString() 和 ToLongDateString() 渲染一堆日期。这些在大多数情况下都可以正常工作,但是在编译应用程序后第一次加载视图时,它们的格式不正确。

我追踪了这一点并检查了当前线程的文化。 99% 的情况下它是 en-US,但在编译后第一次加载 MVC 视图时,它被设置为 en-GB。如果我在那之后立即重新加载页面,它就会回到 en-US。

我尝试将 web.config 文件中的文化和 uiculture 设置为 en-US 以强制其正确,但没有成功。

有人对此有什么想法吗? MVC 中的错误?

编辑(附加代码和尝试): 即使我完全太过分了,将其包含在视图的基类中

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

,并将其包含在 web.config 中

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

,并将其包含在 .aspx 文件的标头中。

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

同样,这仅在编译代码后的初始加载上,当页面首先加载。其他 Web 表单页面不受影响,即使它们源自 System.Web.Mvc.ViewPage。所有后续加载都会正确处理培养物。仅更改 .aspx 文件不会导致此问题,必须编译 C# 代码才能导致此问题。

更多数据: 我已将其追踪到 Render 方法。在 Render 方法之前,区域性是 en-US,之后是 en-GB(同样仅在编译后的初始页面加载时)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

↘紸啶 2024-10-07 12:10:50

在您看来,尝试创建一个基本视图 - 然后为该特定视图继承它,如下所示: 如何全球化 ASP.NET MVC 视图(特别是小数分隔符)?
然而你的更像是:

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

我相信全球化密钥正常工作存在一些问题。

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.

不知在何时 2024-10-07 12:10:50

事实证明,这是由于对过时的第三方 .dll 的依赖造成的。当我找到它并获得更新的 .dll 后,一切又恢复正常了。

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.

痕至 2024-10-07 12:10:50

你如何将文化设置到网络配置中?

您使用“全球化”键吗?

看看:

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

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

心清如水 2024-10-07 12:10:50

我对您的 .aspx 页面的第一次观察是,

您没有从基本 ViewPage 类继承 ASPX 页面。尝试将其添加到您的 ASPX 页面标题标记中。

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

所以它应该看起来像这样

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="XXX.Views.DNViewPage<YYY.Models.zzzViewModel>" %>

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>" %>
游魂 2024-10-07 12:10:50

您是否尝试过制作基础控制器?我在工作中用一个应用程序改变了文化,它运行得很好。

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文