VB.NET查询字符串具有参数

4

我该如何在VB.NET中检查查询字符串是否有参数?我很难将C#代码转换成VB。

我特别想知道如何确定是否存在一个没有值/没有键的参数。

伪代码:

If Request.QueryString.Contains("test") Then
    ' ...
End If

示例:

http://example.com/mypage.aspx?test
http://example.com/mypage.aspx?test=1
http://example.com/mypage.aspx?someparam=1&test&anotherparam=2

澄清一下,我不在乎test是否有值。我只想知道它是否在查询字符串中。

3个回答

2

0

你接近了。使用:

If Request.QueryString.ToString.Contains("test") Then
    ' ...
End If

2
谢谢。这对我的情况很有效,但值得注意的是,如果另一个参数的值恰好包含相同的“test”字符串,即“?someparam=test”,那么也可能会匹配。 - jeffjenx
1
@Quantastical 不应该更加复杂。在这种情况下,除了上述解决方案之外,还应该检查键/值对的实际值。 - Yuriy Galanter
2
这不是完全正确的答案,不应该被接受。 - Honza Zidek

-1

这应该涵盖两种情况:

<%@ Page Language="VB"%>
<%
if Request.QueryString("test")<>"" then
  Response.Write ("EXISTS")
else 
  Response.Write ("not defined")
end if
%>

这个答案是错误的。在http://example.com/mypage.aspx?test存在参数但没有指定任何值时,它不能正确回答。 - Honza Zidek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接