2

我正在开始一项多语言工作。在 Global.asax.cs 方法 Application_BeginRequest() 中,读取 cookie 并将 CurrentUICulture 设置为en-US。但是,在 MyPage.aspx 中,该值令人惊讶地更改为nl.

现在这个网站是我继承的,中等规模,有一个相当复杂的菜单系统。此外,它还维护一个用户表,其中包含一个首选语言字段,其值nl

这是一个带有 MasterPage 的页面,所以我查看了那里。我在 MasterPage.Page_Init() 的第一个页面事件中设置了一个调试断点,并在即时窗口中检查了 System.Threading.Thread.CurrentThread.CurrentUICulture.Name。值为:'nl'。我完全不解。

我的问题是:在 Global.asax.cs、Application_BeginRequest() 和 MasterPage.Page_Init() 之间可以执行哪些代码?

4

1 回答 1

2

处理程序是 Asp.Net 生命周期的Application_BeginRequest()第一步。看这里:

https://docs.microsoft.com/en-us/iis/application-frameworks/building-and-running-aspnet-applications/aspnet-integration-with-iis

当处理程序正在执行时(这是您的页面),它有自己的生命周期:

https://docs.microsoft.com/en-us/previous-versions/ms178472(v=vs.140)?redirectedfrom=MSDN

这个文档说:

开始

在启动阶段,设置请求和响应等页面属性。在这个阶段,页面还会判断请求是回发还是新请求,并设置 IsPostBack 属性。该页面还设置 UICulture 属性。

当检查可用事件时,我们看到:

预初始化

在启动阶段完成后和初始化阶段开始之前引发。

所以; 似乎最早可以改变的阶段UICulturePreInit阶段。然而,最合适的地方是InitializeCulture服务于这个特定目的的方法:

public partial class _Default : Page
{
    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);

        HttpCookie languageCookie = Request.Cookies["lang"];
        if (languageCookie != null)
        {
            if (languageCookie.Value == "en")
            {
                base.Culture = base.UICulture = CultureInfo.GetCultureInfo("en-US").Name;
            }
        }
    }

    protected override void InitializeCulture()
    {
        // Or do it here. This is more appropriate.
    }
    ...
    ...
}

编辑:

尽管跨多个页面共享文化信息,但不应在母版页的事件处理程序中设置文化信息,因为从以下跟踪输出中可以看出,当页面使用母版页时,母版页被解释为页面和Page_Load此母版页的事件在页面处理程序之后执行的 LoadControls() 期间执行Page_Load

我还想在这里提到跟踪功能。通过启用跟踪,可以查看有关页面执行过程、其子级、所有时间、标题等的大量信息:

在 web.config 文件中,在<system.web>

<trace pageOutput="true" requestLimit="10" enabled="true" localOnly="true" traceMode="SortByTime" mostRecent="true"/>

或在此处阅读<system.webServer>等效内容:

https://docs.microsoft.com/en-us/iis/configuration/system.webserver/tracing/

Control Tree
Control UniqueID    Type    Render Size Bytes (including children)  ViewState Size Bytes (excluding children)   ControlState Size Bytes (excluding children)
__Page  ASP.default_aspx    1104    0   0
    ctl00   ASP.masterpage_master   1104    0   0
        ctl00$ctl02 System.Web.UI.LiteralControl    68  0   0
        ctl00$ctl00 System.Web.UI.HtmlControls.HtmlHead 48  0   0
            ctl00$ctl01 System.Web.UI.HtmlControls.HtmlTitle    29  0   0
            ctl00$head  System.Web.UI.WebControls.ContentPlaceHolder    6   0   0
                ctl00$head$ctl00    System.Web.UI.LiteralControl    6   0   0
        ctl00$ctl03 System.Web.UI.LiteralControl    14  0   0
        form1   System.Web.UI.HtmlControls.HtmlForm 954 0   0
            ctl00$ctl04 System.Web.UI.LiteralControl    21  0   0
            ctl00$ContentPlaceHolder1   System.Web.UI.WebControls.ContentPlaceHolder    291 0   0
                ctl00$ContentPlaceHolder1$ctl00 System.Web.UI.LiteralControl    198 0   0
                ctl00$ContentPlaceHolder1$hdn1  System.Web.UI.WebControls.HiddenField   91  0   0
                ctl00$ContentPlaceHolder1$ctl01 System.Web.UI.LiteralControl    2   0   0
            ctl00$ctl05 System.Web.UI.LiteralControl    18  0   0
        ctl00$ctl06 System.Web.UI.LiteralControl    20  0   0
于 2020-03-21T13:21:37.227 回答