c# - 为什么 "new CultureInfo("xx")"在一种环境中引发异常,而在另一种环境中则不会?

标签 c# .net iis globalization

我的 Asp.Net MVC 应用程序面向 .Net Framework 4.6.1

在我的开发环境中,这有效:

var x = new CultureInfo("xx")

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET版本:4.8.4210.0 (我从随机错误页面获得此信息,在同一 MVC 应用程序的其他地方,它在底部提到)

Windows 10 版本 18363.1016 上的 IIS 10.0.18362.650 (从 IIS 管理器获取的信息 - 帮助 - 关于)

在托管的登台(和实时)环境中,相同的代码会引发异常

[CultureNotFoundException: Culture is not supported.
Parameter name: name
xx is an invalid culture identifier.]
   System.Globalization.CultureInfo..ctor(String name, Boolean useUserOverride) +230

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET版本:4.8.3928.0 Windows Server 2012 R2 上的 IIS 8.5.9600.16384

我知道“xx”不是有效的区域性(我只是用它来比较不同语言的资源文件,并且需要一个不存在的区域性)。但我想知道的是为什么这在一种环境下失败而在另一种环境下有效。 asp.net 版本(几乎)相同。

最佳答案

What is happen is when you create a culture with name like "aaa" which is is not one of the names carried by Windows, at that time Windows will try to create it for us and try to fill the culture properties as much it can by matching the language part and country part. for example, if I try to create ja-US which I think not exist, Windows will try to fake the culture by getting the language information from the Japanese language and will get the country information from US and combine it into a new culture. now when passing "aaa" which not matching any language/country, Windows will just use Invariant culture properties for that. And that is what you are seeing.

This is the behavior defined by Windows starting Windows 10 and not really the .NET. the framework just calling the OS for getting the needed information. the code of CreateSpecificCulture is just calling the OS at the end.

Source

关于c# - 为什么 "new CultureInfo("xx")"在一种环境中引发异常,而在另一种环境中则不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63718147/

相关文章:

asp.net-mvc - 如何向 MVC 网站添加第二个应用程序

c# - NVarChar 到 C# 数据类型

c# - WPF 中真正的固定宽度字体

c# - 为什么迭代器 (.Net) 在此代码中不可靠

java - 单独的 cookie 设置是否优先于 web.config 中的设置?

php - 使用错误的 php.ini 文件

python - IIS 7.5 上的 Mercurial 和 hgweb - python 错误

c# - WPF TabItem 标题 - 强制按钮占用整个标题空间

c# - 将两个具有重复值的 List<int> 相交

c# - 如何以编程方式获取 session cookie 名称?