【问题标题】:Can I manually call my RegularExpressionAttribute from controller?我可以从控制器手动调用我的 RegularExpressionAttribute 吗?
【发布时间】:2014-03-17 19:47:57
【问题描述】:

这是个坏主意吗?似乎能够重用正则表达式会很好

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class SafeInputAttribute : RegularExpressionAttribute
{
  /// <summary>
  /// Regular expression for safeinput
  /// </summary>
  public SafeInputAttribute()
    : base(@"[^{}<>]+$")
  {
  }
}

这样的?

    SafeInputAttribute safeInput = new SafeInputAttribute();
    safeInput.Validate(pin, "Login Pin");

它不起作用,安全输入为空。

感谢任何反馈。

【问题讨论】:

    标签: c# regex asp.net-mvc asp.net-web-api


    【解决方案1】:

    您确定safeinput 为空吗?我看不出有什么理由。要手动调用验证器,您可以这样做:

    SafeInputAttribute validator = new SafeInputAttribute();
    bool valid = validator.IsValid(testValue);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 2016-10-09
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多