加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何在Windows Azure网站(asp.net mvc4)上更改验

发布时间:2020-12-16 03:45:11 所属栏目:asp.Net 来源:网络整理
导读:我已经添加了 globalization culture =“de-DE”uiCulture =“de-DE”/到我的Web.config.我将@ Thread.CurrentThread.CurrentCulture添加到我的testview中,它显示了de-DE.所以,一切似乎都很好. 但验证消息仍然是英文,例如 The Input field is required. 我的
我已经添加了< globalization culture =“de-DE”uiCulture =“de-DE”/>到我的Web.config.我将@ Thread.CurrentThread.CurrentCulture添加到我的testview中,它显示了de-DE.所以,一切似乎都很好.

但验证消息仍然是英文,例如

The Input field is required.

我的错是什么?

解决方法

我有同样的问题.

我想Azure“网站”上没有安装“Microsoft .NET Framework 4.5语言包”.似乎使用“Azure云项目”是一个选项,因为您可以直接配置IIS.

另一个解决方案是等待Microsoft在Azure中包含语言包支持…

Personnaly,我决定覆盖主要属性.大多数技巧都是[必需]和[正则表达式].见下文(Loc是我自己的本地化函数,因为我正在使用gettext)

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;

namespace Ic.DataAnnotations
{
    public class RequiredLoc : RequiredAttribute,IClientValidatable
    {

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata,ControllerContext context)
        {
            yield return new ModelClientValidationRule
            {
                ErrorMessage = FormatErrorMessage(metadata.DisplayName),ValidationType = "required"
            };
        }

        public override string FormatErrorMessage(string message)
        {
            base.FormatErrorMessage(message);
            return Localizer.Loc("Le champs '%1' est requis.",message);
        }
    }

}

对于正则表达式:

using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace Ic.DataAnnotations
{
    public class RegularExpressionLoc : RegularExpressionAttribute
    {
        private readonly string _errorMessage;

        public RegularExpressionLoc(string errorMessage,string pattern)
            : base(pattern)
        {
            _errorMessage = errorMessage;
        }

        public override string FormatErrorMessage(string input)
        {
            return Localizer.Loc(_errorMessage);
        }
    }
}

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Ic.DataAnnotations
{
    public class RegularExpressionValidator : DataAnnotationsModelValidator<RegularExpressionAttribute>
    {
        private readonly string _message;
        private readonly string _pattern;

        public RegularExpressionValidator(ModelMetadata metadata,ControllerContext context,RegularExpressionAttribute attribute)
            : base(metadata,context,attribute)
        {
            _pattern = attribute.Pattern;
            _message = attribute.FormatErrorMessage("");
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = _message,ValidationType = "regex"
            };

            rule.ValidationParameters.Add("pattern",_pattern);

            return new[] { rule };
        }
    }
}

并在Global.asax

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RegularExpressionLoc),typeof(RegularExpressionValidator));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读