在ASP.NET中验证QueryStrings

Sai*_*han 3 asp.net validation query-string

是否有一个库可以在我当前的ASP.NET应用程序中使用,以验证queryStrings?

编辑〜使用正则表达式查找模式,如字符串,仅限数字,仅限数字,长度为x的字符串,等等

谢谢

cra*_*ver 5

不知道库,但您可以用来检查查询字符串是否存在:

if (!String.IsNullOrEmpty(Request.Querystring["foo"]))
{
   // check further
}
else
{
   // not there, do something else
}
Run Code Online (Sandbox Code Playgroud)

如果要使用Reglar Expressions进一步验证,可以创建一个接受字符串并返回布尔值的类.

public static Boolean IsValid(String s)
{
    const String sRegEx = @"regex here";

    Regex oRegEx = new Regex(sRegEx , RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
    MatchCollection oMatches = oRegEx.Matches(s);

    return (oMatches.Count > 0) ? true : false;
}
Run Code Online (Sandbox Code Playgroud)

这是一个很好的免费程序,可以帮助您构建regluar表达式:Expresso