我在Azure中部署的ASP.NET WebApi中遇到模型验证错误的问题。
这是返回ModelState错误的代码:
public static class ComprobarErroresModelo
{
public static string GetErrors(ModelStateDictionary modelState)
{
var result = "";
foreach (var error in modelState.Values.SelectMany(value => value.Errors))
if (string.IsNullOrEmpty(error.ErrorMessage))
result = result + error.Exception.Message + '\n';
else
result = result + error.ErrorMessage + '\n';
return result;
}
}
在global.asax中,我尝试了几处修改:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es");
Thread.CurrentThread.CurrentCulture = new CultureInfo("es");
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("es");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("es");
}
void Application_BeginRequest(Object sender, EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es");
Thread.CurrentThread.CurrentCulture = new CultureInfo("es");
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("es");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("es");
}
但是没有一个在Azure中工作,我总是在英语中得到ModelState错误
我已经在本地环境中测试了“es”到“en”,它运行正常,但在Azure中它总是显示英文消息错误
如何在Azure Web角色中以西班牙语或其他语言获取ModelState错误?
答案 0 :(得分:0)
您可以创建一个BaseController
,它在初始化时具有文化集,并使所有API控制器直接从而继承而不是ApiController
:
public abstract class BaseController : ApiController
{
protected override void Initialize(HttpControllerContext requestContext)
{
base.Initialize(requestContext);
var culture = new CultureInfo("es");
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
}
}