Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#来自常数的FluentValidation读取模式不工作_C#_Regex_Asp.net Mvc_Fluentvalidation - Fatal编程技术网

C#来自常数的FluentValidation读取模式不工作

C#来自常数的FluentValidation读取模式不工作,c#,regex,asp.net-mvc,fluentvalidation,C#,Regex,Asp.net Mvc,Fluentvalidation,我在从类中读取静态字符串时遇到问题,该类保存我的模式以进行验证。 当我调试时,我看到大括号被转换成方括号。 这是我的班级: public static class ArticleConstant { public const int LengthOfArticleNumber = 9; public static readonly string PatternArticleNumber = "^[A-Z0-9]{9}$"; } 我也试过了 public static reado

我在从类中读取静态字符串时遇到问题,该类保存我的模式以进行验证。 当我调试时,我看到大括号被转换成方括号。 这是我的班级:

public static class ArticleConstant
{
    public const int LengthOfArticleNumber = 9;
    public static readonly string PatternArticleNumber = "^[A-Z0-9]{9}$";
}
我也试过了

public static readonly string PatternArticleNumber = "^[A-Z0-9]\{9\}$";
public static readonly string PatternArticleNumber = "^[A-Z0-9]{{9}}$";
public static readonly string PatternArticleNumber = @"^[A-Z0-9]{9}$";
public static readonly string PatternArticleNumber = $"^[A-Z0-9]{9}$";
但这些都不起作用。 这是我的实际验证规则:

this.RuleFor(article => article.Number)
            .NotEmpty()
            .WithLocalizedMessage(() => ValidationErrorMessages.IsRequired)
            .Length(ArticleConstant.LengthOfArticleNumber)
            .WithLocalizedMessage(() => ValidationErrorMessages.DefinedSized)
            .Matches(ArticleConstant.PatternArticleNumber)
            .WithLocalizedMessage(() => ValidationErrorMessages.MustNotContainAnySpecialCharacter);
当我将模式直接输入Matches函数时,一切都像预期的那样工作

有人能给我解释一下这里发生了什么事吗

谢谢,
大卫

我现在用

public const string PatternArticleNumber = "^[A-Z0-9]{9}$";

似乎
静态只读
正在执行某些操作,因此验证失败。

我现在使用

public const string PatternArticleNumber = "^[A-Z0-9]{9}$";

似乎
static readonly
正在执行某些操作,因此验证失败。

您是否尝试过
/^[A-Z0-9]{9}$/
尝试过它。大括号已正确解析,但验证仍然失败。即使使用非常简单的模式,如
public static readonly string PatternArticleNumber=“^[a-Z0-9]*”
验证也会失败。您是否尝试过
/^[a-Z0-9]{9}$/
。大括号已正确解析,但验证仍然失败。即使使用非常简单的模式,如
公共静态只读字符串PatternArticleNumber=“^[a-Z0-9]*”
验证也会失败。