asp.net – 数据注释 – 使用属性扩展并在资源文件中存储正则表达式
发布时间:2020-09-05 01:01:55 所属栏目:asp.Net 来源:互联网
导读:我目前正在使用MVC4数据注释来处理验证.我正在开发一个非常国际化的网站,因此我将所有文本保存在资源文件中. 我还想在资源文件中保留正则表达式以进行验证,这样我就可以使用相同的代码来检查,例如,Post Codes(UK)和Zip Codes(US),只需使用不同的RegEx(以及不
|
我目前正在使用MVC4数据注释来处理验证.我正在开发一个非常国际化的网站,因此我将所有文本保存在资源文件中. 我还想在资源文件中保留正则表达式以进行验证,这样我就可以使用相同的代码来检查,例如,Post Codes(UK)和Zip Codes(US),只需使用不同的RegEx(以及不同名称的资源等) ). 我有以下属性已经从资源文件中提取错误消息.我怎样才能从资源文件中获取正则表达式? [RegularExpression(@"^[w]{1,2}[0-9]{1,2}[w]?s?[0-9]{1,2}[w]{1,2}$",ErrorMessageResourceType = typeof(Resources.ValidationMessages),ErrorMessageResourceName = "validPostcode")]
编辑(再次) 我现在在哪里 按照下面的答案和一些额外的搜索,我有以下内容: 在Global.asax.cs中,我添加了以下行以确保调用客户端验证 DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalisedAttribute),typeof(RegularExpressionAttributeAdapter)); 在我的模型中,我调用了属性扩展 [Localised(typeof(Resources.FormValidation),"postcodeRegEx","postcodeMsg")] 最后,本地化正则表达式验证的属性扩展 public class LocalisedAttribute : RegularExpressionAttribute
{
public LocalisedAttribute(Type resource,string regularExpression,string errorMessage)
: base(GetRegex(regularExpression))
{
ErrorMessageResourceType = resource;
ErrorMessageResourceName = errorMessage;
}
private static string GetRegex(string value)
{
return Resources.FormValidation.ResourceManager.GetString(value);
}
}
这是有效的,但只是我第一次在启动应用程序时使用它. 我将打开另一个问题来解决这个问题 – 它与原始请求没有直接关系,似乎与大多数人的实现没有关系,似乎并不特定于数据注释. 解决方法我已经有一些扩展的RegularExpressionAttribute实现,允许使用资源进行正则表达式模式.看起来像:public class RegularExpressionExAttribute : RegularExpressionAttribute,IClientValidatable
{
private Regex regex { get; set; }
private string pattern;
private string resourceName;
private Type resourceType;
/// <summary>
/// constructor,calls base with ".*" basic regex
/// </summary>
/// <param name="resName">resource key</param>
/// <param name="resType">resource type</param>
public RegularExpressionExAttribute(string resName,Type resType)
: base(".*")
{
resourceName = resName;
resourceType = resType;
}
/// <summary>
/// override RegularExpressionAttribute property
/// </summary>
public new string Pattern
{
get
{
SetupRegex();
return pattern;
}
}
/// <summary>
/// loads regex from resources
/// </summary>
private void SetupRegex()
{
ResourceAccessor ra = new ResourceAccessor(resourceName,resourceType);
pattern = ra.resourceValue;
regex = new Regex(pattern);
}
/// <summary>
/// override validation with our regex
/// </summary>
/// <param name="value">string for validation</param>
/// <returns></returns>
public override bool IsValid(object value)
{
SetupRegex();
string val = Convert.ToString(value);
if (string.IsNullOrEmpty(val))
return true;
var m = regex.Match(val);
return (m.Success && (m.Index == 0));
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metaData,ControllerContext controllerContext)
{
yield return new ModelClientValidationRegexRule(base.ErrorMessageString,this.Pattern);
}
}
此外,它还使用ResourceAccessor类从资源中获取正则表达式 public class ResourceAccessor
{
private string resourceName;
private Type resourceType;
private Func<string> accessor;
private string _resourceValue;
public ResourceAccessor(string resourceName,Type resourceType)
{
this.resourceName = resourceName;
this.resourceType = resourceType;
}
public string resourceValue
{
get
{
SetupAccessor();
return accessor();
}
}
private void SetupAccessor()
{
if (accessor != null) //already set
return;
string localValue = _resourceValue;
bool flag1 = !string.IsNullOrEmpty(resourceName);
bool flag2 = !string.IsNullOrEmpty(localValue);
bool flag3 = resourceType != (Type)null;
if (flag1 == flag2)
{
throw new InvalidOperationException("Can't set resource value");
}
if (flag3 != flag1)
{
throw new InvalidOperationException("Resource name and type required");
}
if (flag1)
PropertyLookup();
else
{
accessor = (Func<string>)(() => localValue);
}
}
private void PropertyLookup()
{
if (resourceType == (Type)null || string.IsNullOrEmpty(resourceName))
{
throw new InvalidOperationException("Resource name and type required");
}
PropertyInfo property = resourceType.GetProperty(resourceName,BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (property != (PropertyInfo)null)
{
MethodInfo getMethod = property.GetGetMethod(true);
if (getMethod == (MethodInfo)null || !getMethod.IsAssembly && !getMethod.IsPublic)
property = (PropertyInfo)null;
}
if (property == (PropertyInfo)null)
{
throw new InvalidOperationException("Resource type doesn't have property");
}
else if (property.PropertyType != typeof(string))
{
throw new InvalidOperationException("Resource type must be string");
}
else
{
accessor = (Func<string>)(() => (string)property.GetValue((object)null,(object[])null));
}
}
}
以下是使用示例: public class SignUpInput
{
[RegularExpressionEx("EmailValidationRegex",typeof(LocalizedResources),ErrorMessageResourceType = typeof(Messages),ErrorMessageResourceName = "invalidEmail")]
public string Email { get; set; }
} (编辑:哈尔滨站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在ASP.Net MVC应用程序中放置初始化代码的位置?
- asp.net-mvc-3 – ASP.Net MVC 3:在哪里处理会话丢失?
- asp.net-mvc – 使用ASP.Net MVC中的模型绑定器更新父/子记
- asp.net-mvc – 从视图到控制器POST信用卡数据是否安全?
- asp.net-mvc – MVC应用程序调试错误:viewstate MAC的验证
- asp.net – NHibernate – ManagedWebSessionContext和WebS
- asp.net-mvc – 通过Gitignore递归地包含Nuget DLL
- asp.net – VB.NET – 如何使用Active Directory将SID转换为
- asp.net – Orchard CMS Media中的文件大小上传限制
- asp.net – 在为app_offline.htm提供特定URL时,将http状态5
推荐文章
站长推荐
- asp.net-core – ASP.Net 5类库中的EntityFramew
- asp.net – 网站随时随地突破
- asp.net – 如何查看Chrome开发者工具中发布到表
- asp.net-mvc – 如何在RegularExpression中忽略大
- asp.net – 部分视图呈现按钮点击
- asp.net-mvc – MVC导航到不同的控制器视图
- asp.net-mvc – ASP.Net [HiddenInput]数据属性在
- asp.net-mvc – 允许asp.net mvc 2控制器名称的U
- asp.net-mvc – URL中的ASP.NET MVC冒号
- linq – ASP.NET Web API GET方法:为单个参数传
热点阅读
