C# 获取'的错误;IsNullOrWhiteSpace';
我使用了下面这样的代码C# 获取'的错误;IsNullOrWhiteSpace';,c#,asp.net,C#,Asp.net,我使用了下面这样的代码 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (string.IsNullOrWhiteSpace(Request.QueryString["tx"]) == false) { if (Regex.IsMatch(HttpUtility.UrlDecode(Request.QueryS
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (string.IsNullOrWhiteSpace(Request.QueryString["tx"]) == false)
{
if (Regex.IsMatch(HttpUtility.UrlDecode(Request.QueryString["tx"]), "[^a-zA-Z0-9 % +]"))
{
//error
Response.Redirect("Error.aspx");
}
else
{
SearchResult();
}
}
}
}
但我在这一点上犯了错误
if(string.IsNullOrWhiteSpace(Request.QueryString[“tx”])==false)
as“字符串”不包含“IsNullOrWhiteSpace”的定义。
此外,我还使用了相关的名称空间
我正在使用asp.net 2.0版,无法更改。请帮助解决此问题需要做些什么。
String.IsNullOrWhiteSpace
是在.NET 4.0中引入的:
如果您真的不能使用更高版本,那么您可以构建自己的方法来做同样的事情
下面是该方法的实现(感谢Farhad Jabiyev):
公共静态bool IsNullOrWhiteSpace(字符串值){
if(value==null)返回true;
for(int i=0;i
注意:我已经删除了[Pure]
属性,该属性存在于上述链接的实现中,因为System.Diagnostics.Contracts.PureAttribute
您可以像这样检查:
if (Request.QueryString["tx"] != null && Request.QueryString["tx"].Trim() != "")
正如roryap所说,
String.IsNullOrWhiteSpace
不可用
String.IsNullOrEmpty
是,这可能适合您的需要
if (!string.IsNullOrEmpty(Request.QueryString["tx"]))
您可以构建自己的
IsNullOrWhiteSpace
:
public static bool IsNullOrWhiteSpace(string input)
{
if (input == null || input == String.Empty) return true;
foreach (char c in input)
if (!Char.IsWhiteSpace(c))
return false;
return true;
}
以下是提示:您可以将此添加到您的答案中。@FarhadJabiyev--谢谢,我正试图查找此内容,但连接不稳定:)我已更新了我的答案。@FarhadJabiyev:谢谢您提供的文档@NadeemKhan——尝试添加
System.Diagnostics.Contracts
@NadeemKhan——这也一定是更高版本的东西。只需将[Pure]
全部删除即可。您可以先使用IsNullOrEmpty,然后检查空白。将检查此项并让您知道……)IsNullOrEmpty
与IsNullOrWhiteSpace
不同,因此如果OP需要后一种方法的功能,他必须构建一个自定义方法。同意;我说过“可能适合你的需要”而不是“将作为一个确切的替代品”。谢谢蒂姆的解释。我应该如何在我的code@NadeemKhan:您只需将上面的粘贴方法复制到一个可以从需要的位置访问的类中。当然,将执行相同的操作并进行检查。是否有其他建议,说明为什么即使应用了该方法,我也会出现相同的错误??
public static bool IsNullOrWhiteSpace(string input)
{
if (input == null || input == String.Empty) return true;
foreach (char c in input)
if (!Char.IsWhiteSpace(c))
return false;
return true;
}