What is the type of Request.QueryString() and how

2019-09-02 09:59发布

问题:

Maybe it's trivial question, but I have no idea what is the type of Request.QueryString(). When I check it with typeof - it says it's Object. How to check which object it is and which property of that object is a string in URL?

I'm doing it server side with language specification <%@ 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? 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?

回答1:

The Request collection is an object that inherits IRequestDictionary interface.
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.
You said you know C# so you'll understand the following fictitious QueryString declaration.

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

And a few examples how you should check the values in JScript without string conversion.

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
}


回答2:

The result of Request.Querystring (with no name provided) is a string. The result of Request.Querystring("name") depends on whether "name" is part of the querystring, and if it is, whether it has a value.

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, i.e. both Empty = "" and Empty = 0 are in some sense true).