Asp.net 设置cultureinfo不会从本地资源文件中提供正确的字符串

Asp.net 设置cultureinfo不会从本地资源文件中提供正确的字符串,asp.net,webforms,culture,Asp.net,Webforms,Culture,我有一个名为buystuff.aspx的网站。我创建了两个本地资源:buystuff.aspx.resx和buystuff.aspx.da dk.resx 这很好,如果我使用da DK设置进入站点,我会得到该版本,如果我使用其他设置进入站点,我会得到默认版本 但是,我想要的是以编程方式设置它。当用户输入buystuff.aspx时,他们应该被强制输入英语(en-US)版本,如果他们输入buystuff.aspx?language=da,他们应该被强制输入da-dk版本 以下代码不起作用: pri

我有一个名为buystuff.aspx的网站。我创建了两个本地资源:buystuff.aspx.resxbuystuff.aspx.da dk.resx

这很好,如果我使用da DK设置进入站点,我会得到该版本,如果我使用其他设置进入站点,我会得到默认版本

但是,我想要的是以编程方式设置它。当用户输入buystuff.aspx时,他们应该被强制输入英语(en-US)版本,如果他们输入buystuff.aspx?language=da,他们应该被强制输入da-dk版本

以下代码不起作用:

private void SetupLanguage()
{
    if (!string.IsNullOrEmpty(CurrentLanguage))
    {
        if (CurrentLanguage == "da")
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
            return;
        }
    }
    Culture = "en-US";
    UICulture = "en-US";
}
我还尝试了以下不起作用的方法:

private void SetupLanguage()
{
    if (!string.IsNullOrEmpty(CurrentLanguage))
    {
        if (CurrentLanguage == "da")
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
            return;
        }
    }
    CultureInfo info = CultureInfo.CreateSpecificCulture("en-US");
    Thread.CurrentThread.CurrentUICulture = info;
    Thread.CurrentThread.CurrentCulture = info;
}
在调试模式下,我可以判断代码是否正常运行。但是,当加载buybtc.aspx(并且我的CurrentLanguage变量是string.empty)时,它仍然从buystuff.aspx.da-dk.resx加载资源

有什么想法吗?

好的,我自己解决了(那些时间一去不复返了,永远都没有了)

问题出在我的web.config中,我有几个SEO规则:

 <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
          <match url="*"/>
          <conditions>
            <add input="{HTTP_HOST}" pattern="btcglobe.com"/>
          </conditions>
          <action type="Redirect" url="http://www.btcglobe.com/{R:0}"/>
        </rule>
        <!--To always remove trailing slash from the URL-->
        <rule name="Remove trailing slash" stopProcessing="true">
          <match url="(.*)/$"/>
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="{R:1}"/>
        </rule>
        <rule name="LowerCaseRule1" stopProcessing="true">
          <match url="[A-Z]" ignoreCase="false"/>
          <conditions>
            <add input="{URL}" matchType="Pattern" pattern="^.+\.((axd)|(js)|(xaml)|(asmx))$" ignoreCase="true" negate="true"/>
          </conditions>
          <action type="Redirect" url="{ToLower:{URL}}"/>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

为什么你只在一个地方设置
CurrentUICulture
?@Oded,你是什么意思?:-)在第二个代码示例中,您有
Thread.CurrentThread.CurrentUICulture=info。您从未在任何其他地方设置过
CurrentUICulture
。您应该同时将它(和
CurrentCulture
设置为所需的区域性。我可能很笨或很慢,但为什么要多次设置CurrentUICulture?页面加载一次还不够吗?我想我还不明白,您能不能:
private void SetupLanguage()
{
    if (!string.IsNullOrEmpty(CurrentLanguage))
    {
        if (CurrentLanguage == "da")
        {
            CultureInfo dkinfo = CultureInfo.CreateSpecificCulture("da-dk");
            CultureInfo.DefaultThreadCurrentCulture = dkinfo;
            CultureInfo.DefaultThreadCurrentUICulture = dkinfo;
            Thread.CurrentThread.CurrentCulture = dkinfo;
            Thread.CurrentThread.CurrentUICulture = dkinfo;

            return;
        }
    }
    CultureInfo info = CultureInfo.CreateSpecificCulture("en-us");
    CultureInfo.DefaultThreadCurrentCulture = info;
    CultureInfo.DefaultThreadCurrentUICulture = info;
    Thread.CurrentThread.CurrentCulture = info;
    Thread.CurrentThread.CurrentUICulture = info;

}