在ASP.NET Core MVC中配置客户端验证的文化

时间:2017-12-08 15:05:13

标签: c# validation asp.net-core asp.net-core-mvc

我很难在我的APP中配置客户端验证。我希望它能够接受本地化的file输入。

我尝试在启动时使用配置应用程序:

pt-BR

我将var locale = "pt-BR"; RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions { SupportedCultures = new List<CultureInfo> { new CultureInfo(locale) }, SupportedUICultures = new List<CultureInfo> { new CultureInfo(locale) }, DefaultRequestCulture = new RequestCulture(locale) }; 设置为主HTML文件。

它适用于在服务器端呈现货币,日期时间和数字,但客户端验证仍然期望输入<html lang="pt-br">

客户端正在使用en-USjquery.validate

如何配置客户端验证以了解本地化输入?

1 个答案:

答案 0 :(得分:1)

创建一个这样的辅助类:

public class PortugueseCulture : CultureInfo
{
    private readonly Calendar cal;
    private readonly Calendar[] optionals;

    public PortugueseCulture()
        : this("pt-BR", true)
    {
    }

    public PortugueseCulture(string cultureName, bool useUserOverride) : base(cultureName, useUserOverride)
    {
        //Your Custom Currency Numbers Calendar Culture Code
    }

    public override Calendar Calendar
    {
        get { return cal; }
    }

    public override Calendar[] OptionalCalendars
    {
        get { return optionals; }
    }
}

然后在Global.asax.cs中添加此方法

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var portugueseCulture = new PortugueseCulture();
        Thread.CurrentThread.CurrentCulture = portugueseCulture;
        Thread.CurrentThread.CurrentUICulture = portugueseCulture;
    }