Switching ASP.Net MVC Culture after Compilation for Bootstrap

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

I followed this up and tested the current flow culture. In 99% of cases, it is in en-US mode, but the first time the MVC view is loaded after compilation, it is set to en-GB. If I reload the page right after that, return it to en-US.

I tried to set the culture and beekeeping in the web.config file in en-US to make it be correct, but no luck.

Anyone have any ideas on this? Error in MVC?

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

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 it in the web.config file

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

and this is 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 bootstrapping after compiling the code, when this page loads first. Other web form pages are not affected, even if they descend from System.Web.Mvc.ViewPage. All subsequent loads process the culture correctly. Just changing the .aspx file does not cause this, C # code needs to be compiled to trigger this.

Additional data: I tracked it to the Render method. Before the Render method, the culture is en-US, and then it is en-GB (again, only at boot after compilation).

+3
5

, .dll. .dll, .

+1

- , : ASP.NET MVC ( , )? :

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

, .

+1

? , .

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

.aspx,

ASPX ViewPage. ASPX.

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

:

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

Source: https://habr.com/ru/post/1772938/


All Articles