简体   繁体   English

Request.QueryString()的类型是什么?如何检查它是否为空?

[英]What is the type of Request.QueryString() and how to check if it's empty?

Maybe it's trivial question, but I have no idea what is the type of Request.QueryString() . 也许这是一个琐碎的问题,但是我不知道Request.QueryString()的类型是什么。 When I check it with typeof - it says it's Object. 当我用typeof检查它时-它说是Object。 How to check which object it is and which property of that object is a string in URL? 如何检查它是哪个对象,以及该对象的哪个属性是URL中的字符串?

I'm doing it server side with language specification <%@ language="javascript"%> 我正在服务器端使用语言规范<%@ language="javascript"%>

If I've got URL like that: http://127.0.0.1/Kamil/Default.asp?name= then how to check if name is empty? 如果我有这样的URL: http : //127.0.0.1/Kamil/Default.asp ?name=,那么如何检查名称是否为空? It's not null. 不为空。 I know I can convert Request.QueryString("name") to String and check if it's empty string "", but is it proper way? 我知道我可以将Request.QueryString("name")转换为String并检查它是否为空字符串“”,但这是正确的方法吗?

The result of Request.Querystring (with no name provided) is a string. Request.Querystring的结果(未提供名称)是一个字符串。 The result of Request.Querystring("name") depends on whether "name" is part of the querystring, and if it is, whether it has a value. Request.Querystring("name")取决于“ name”是否为查询字符串的一部分,如果是,则是否具有值。

So, given the following querystring: 因此,给定以下查询字符串:

 mypage.asp?A=1&B=

If you read them into variables: 如果将它们读入变量:

x = Request.Querystring("A")
y = Request.Querystring("B")
z = Request.Querystring("C")

you'll get the following results: x = "1" (a string), y = "" (a blank string), and z = Empty (a special value which will be silently converted to a string or a number depending on how you use it, ie both Empty = "" and Empty = 0 are in some sense true). 您将得到以下结果:x = "1" (一个字符串),y = "" (一个空白字符串)和z = Empty (一个特殊值,它将根据如何将其默默转换为字符串或数字)您使用它,即Empty = ""Empty = 0在某种意义上都是正确的)。

The Request collection is an object that inherits IRequestDictionary interface. Request集合是一个继承IRequestDictionary接口的对象。
In JScript, it's a good practice to use item to get actual value, not the implicit one due to values of the QueryString collection (also Form and ServerVariables ) are IStringList in fact. 在JScript中,最好使用item来获取实际值,而不是隐式值(由于QueryString集合的值(同样是FormServerVariables )实际上是IStringList )。
You said you know C# so you'll understand the following fictitious QueryString declaration. 您说您知道C#,所以您将了解以下虚拟QueryString声明。

var QueryString = new IRequestDictionary<string, IStringList>();

And a few examples how you should check the values in JScript without string conversion. 还有一些示例,您应该如何在不进行字符串转换的情况下检查JScript中的值。

if(Request.QueryString("name").count==0){
    // parameter does not exist
}

if(Request.QueryString("name").item==undefined){
    // parameter does not exist
}

if(Request.QueryString("name").item!=undefined){
    // parameter exists and may be empty
}

if(Request.QueryString("name").item){
    // parameter exists and non-empty
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM