Asp.net mvc 将线程区域性设置为中性区域性
尝试使用db_ent.SaveChanges将数据保存到数据库时,出现以下错误: 文化“sr”是一种中性文化。它不能用于格式化和解析,因此不能设置为线程的当前区域性。有什么解决办法吗 编辑: 我的常规本地化助手Asp.net mvc 将线程区域性设置为中性区域性,asp.net-mvc,Asp.net Mvc,尝试使用db_ent.SaveChanges将数据保存到数据库时,出现以下错误: 文化“sr”是一种中性文化。它不能用于格式化和解析,因此不能设置为线程的当前区域性。有什么解决办法吗 编辑: 我的常规本地化助手 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; using Syste
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Threading;
using System.Globalization;
namespace darns.Helpers
{
public class MultiCultureMvcRouteHandler : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
{
String culture = requestContext.RouteData.Values["culture"].ToString();
var ci = new CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
return base.GetHttpHandler(requestContext);
}
}
public class SingleCultureMvcRouteHandler : MvcRouteHandler {}
public class CultureConstraint : IRouteConstraint
{
private string[] _values;
public CultureConstraint(params string[] values)
{
this._values = values;
}
public bool Match(HttpContextBase httpContext,Route route,string parameterName,
RouteValueDictionary values, RouteDirection routeDirection)
{
// Get the value called "parameterName" from the
// RouteValueDictionary called "value"
string value = values[parameterName].ToString();
// Return true is the list of allowed values contains
// this value.
return _values.Contains(value);
}
}
public enum Culture
{
sr = 1,
hr = 2,
en = 3,
ru = 4
}
/// <summary>
/// Util class for storing error message pairs
/// </summary>
/// <typeparam name="E"></typeparam>
/// <typeparam name="T"></typeparam>
public class ErrorPair<E, T>
{
public E Key { get; set; }
public T Message { get; set; }
public ErrorPair(E key, T message)
{
Key = key;
Message = message;
}
}
}
您不能使用中性文化sr、en、fr等。。。因为特定区域性中可能存在格式差异。例如,en US和en GB可能会以不同的格式设置日期,仅使用en无法确保格式设置正确
你必须使用特定的文化。在您的情况下,请尝试使用“sr Cyrl CS”塞尔维亚语、西里尔语或“sr Latn CS”塞尔维亚语、拉丁文化,或者您知道应该使用的其他sr特定文化名称。您不能使用中性文化sr、en、fr等。。。因为特定区域性中可能存在格式差异。例如,en US和en GB可能会以不同的格式设置日期,仅使用en无法确保格式设置正确
你必须使用特定的文化。在你的例子中,不要只使用“sr”来代替“sr”来代替“sr-Cyrl-CS”塞尔维亚语、西里尔语或“sr-Latn-CS”塞尔维亚语,而是使用拉丁文化,或者你知道应该使用的另一种特定于sr的文化名称。用sr-Cyrl-RS来代替西里尔语,或者用sr-Latn-RS来代替拉丁语。我也有类似的问题。用户的浏览器正在发送“en”中性文化 所以我用这个来让.Net选择最接近的“特定”文化:
//replace '$' in string with current user's culture's Currency Symbol
//if neutral culture use .Net to set SpecificCulture
//, e.g. 'en', .Net assumes 'en-US'
private static MvcHtmlString currencySubstitution(MvcHtmlString htmlString)
{
var ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
System.Globalization.RegionInfo ri;
if (ci.IsNeutralCulture)
{
ci = System.Globalization.CultureInfo.CreateSpecificCulture(ci.TwoLetterISOLanguageName);
}
ri = new System.Globalization.RegionInfo(ci.LCID);
string currencySymbol = ri.CurrencySymbol;//en-AU: $,or // AUD = ri.ISOCurrencySymbol;
return MvcHtmlString.Create(htmlString.ToHtmlString().Replace("$", currencySymbol));
}
我也有类似的问题。用户的浏览器正在发送“en”中性文化 所以我用这个来让.Net选择最接近的“特定”文化:
//replace '$' in string with current user's culture's Currency Symbol
//if neutral culture use .Net to set SpecificCulture
//, e.g. 'en', .Net assumes 'en-US'
private static MvcHtmlString currencySubstitution(MvcHtmlString htmlString)
{
var ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
System.Globalization.RegionInfo ri;
if (ci.IsNeutralCulture)
{
ci = System.Globalization.CultureInfo.CreateSpecificCulture(ci.TwoLetterISOLanguageName);
}
ri = new System.Globalization.RegionInfo(ci.LCID);
string currencySymbol = ri.CurrencySymbol;//en-AU: $,or // AUD = ri.ISOCurrencySymbol;
return MvcHtmlString.Create(htmlString.ToHtmlString().Replace("$", currencySymbol));
}
当我发布数据时,是否可能在控制器操作中更改非特定区域性?我不是100%确定,但我不认为在操作中设置区域性,然后立即尝试保存数据会起作用,因为在保存数据之前,区域性更改不会在上下文/线程中完成。在进行任何数据处理之前,您确实想知道您使用的是什么文化。这里有一些关于让MVC改变文化的链接。。。。有没有办法禁用此区域性检查?我不能使用sepcific区域性,因为我的本地化代码非常复杂,无法更改。当我发布数据时,是否可以在控制器操作中更改非特定区域性?我不是100%确定,但我认为在操作中设置区域性,然后立即尝试保存数据不会奏效,因为区域性更改不会在上下文/线程中完成直到发生数据保存之后。在进行任何数据处理之前,您确实想知道您使用的是什么文化。这里有一些关于让MVC改变文化的链接。。。。有没有办法禁用此区域性检查?我不能使用sepcific文化,因为我的本地化代码很难更改