ASP.NET Globalization: Culture=“auto” page directive with neutral culture crash?

旧巷老猫 提交于 2019-12-09 17:17:57

问题


I'm running into a case where an ASP.NET application using the built-in globalization facilities is crashing.

On an ASP.NET page with the Culture="auto" directive, a user with a neutral culture as their browser language (such as "zh-Hans") will produce the following exception:

Culture 'zh-Hans' is a neutral culture. It cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture.

at System.Globalization.CultureInfo.CheckNeutral(CultureInfo culture) at System.Threading.Thread.set_CurrentCulture(CultureInfo value) at System.Web.UI.Page.set_Culture(String value) at ASP.somePage_aspx.__BuildControlTree(somePage_aspx __ctrl) at ASP.somePage_aspx.FrameworkInitialize()

Any ideas? Garbage fed into the Culture/UICulture parameters generally seem to be ignored, but this case is causing an unhandled exception.


回答1:


First off, you might consider setting UICulture="auto" as well as Culture="auto" in your <%@ Page %> declaration.

Now, I am not seeing this repro on my .NET 4.0 (beta) install, so this might be a product bug in .NET 3.5.

Here's a great resource for learning about neutral cultures and the difference between UICulture and Culture: http://blogs.msdn.com/ddietric/archive/2008/02/05/yacvcp-yet-another-currentculture-vs-currentuiculture-post.aspx

Hope that's helpful.




回答2:


I was having the same problem and after bonking my head against a wall for a while found the answer right under my nose.

The issue I had was in not understanding the difference between CurrentCulture and CurrentUICulture. The difference being CurrentCulture is used to format dates, numbers and perform sorting, CurrentUICulture is used to lookup culture specific strings from a resource.

I had some code that looked like

return input.ToString("C", System.Globalization.CultureInfo.CurrentUICulture);

when it should be been

return input.ToString("C", System.Globalization.CultureInfo.CurrentCulture);

When you start trying to format culture specific items with a non-specific culture you will get the System.NotSupportedException.




回答3:


Can't you set the culture on begin request? (Note: asp.net requests can jump between threads so you need to hook into the thread moving as well.)



来源:https://stackoverflow.com/questions/1314627/asp-net-globalization-culture-auto-page-directive-with-neutral-culture-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!