ModelState错误总是在Azure Web角色中以英文显示

本文关键字:文显示 显示 角色 错误 Web Azure ModelState | 更新日期: 2023-09-27 17:49:18

我在ASP中遇到模型验证错误的问题。. NET WebApi在Azure中部署。

这是返回ModelState Errors的代码:
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;
    }
}

在全球。我已经尝试了几个修改:

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 Role中获得西班牙语或其他语言的ModelState错误??

ModelState错误总是在Azure Web角色中以英文显示

您可以创建一个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;
    }
}