• ASP基础之内置类型及注意点


    我记得:

    初学时,经常对Request的类型迷惑

    初学时,经常对Request.Form的类型迷惑

    初学时,经常对Request.Form("id")的类型迷惑

    初学时,经常对.item, .items, .count, .content的表达迷惑不已.

    其实,说不清道不明的原因还是自己太懒惰了,资料查得少,查得略,查得3心2意,自然失过错过.

    很简单的事情 :

     1 <%
    2 On Error Resume Next
    3 Dim i,biName,arrBI,AI
    4 biName = "ObjectContext," &_
    5 "Server," &_
    6 "Application," &_
    7 "Application.Contents," &_
    8 "Application.StaticObjects," &_
    9 "Session," &_
    10 "Session.Contents," &_
    11 "Session.StaticObjects," &_
    12 "Request," &_
    13 "Response," &_
    14 "Server.GetLastError," &_
    15 "Request.ServerVariables," &_
    16 "Request.ClientCertificate," &_
    17 "Request.Cookies," &_
    18 "Request.Form," &_
    19 "Request.QueryString," &_
    20 "Response," &_
    21 "Response.Cookies," &_
    22 "Request.Form(""ID"")," &_
    23 "Request.QueryString(""ID"")," &_
    24 "Request.Cookies(""ID"")," &_
    25 "Request.ClientCertificate(""Subject"")," &_
    26 "Request.ServerVariables(""URL"")," &_
    27 "Server.CreateObject(""ADODB.Connection"")," &_
    28 "Server.CreateObject(""ADODB.Recordset"")," &_
    29 "Server.CreateObject(""Scripting.FileSystemObject"")," &_
    30 "Server.CreateObject(""Scripting.Dictionary"")"
    31
    32 arrBI = Split(biName,",")
    33 Response.Write("<table border=1 width='80%' align='center'> "& _
    34 "<tr><th width='25%' nowrap='nowrap'>TypeName操作</th>" &_
    35 "<th width='12%' nowrap='nowrap'>TypeName结果</th>" &_
    36 "<th width='40%' nowrap='nowrap'>是否有" &_
    37 "Items,Item,Contents,Count属性</th></tr>" & vbNewLine)
    38
    39 For i = 0 To Ubound(arrBI)
    40 Set AI = Eval(arrBI(i))
    41 Response.Write("<tr><td align=right>" &_
    42 "<span style=""color:red"">"&arrBI(i)&"<"&"/"& _
    43 "span>&nbsp;</td><td align=left>" &_
    44 "&nbsp;"&TypeName(AI)&"</td>")
    45
    46
    47 If Err > 0 Then Err.Clear
    48 Dim v,hasItem,hasItems
    49 hasItem = False
    50 hasItems = False
    51 hasContents = False
    52 hasCount = False
    53
    54 v = TypeName(AI("0"))
    55 If Err > 0 Then
    56 Err.Clear
    57 Else
    58 hasItem = "<font color=red><b>True</b></font>"
    59 End If
    60
    61 v = TypeName(AI.Items)
    62 If Err > 0 Then
    63 Err.Clear
    64 Else
    65 hasItems = "<font color=orange><b>True</b></font>"
    66 End If
    67
    68 v = TypeName(AI.Contents)
    69 If Err > 0 Then
    70 Err.Clear
    71 Else
    72 hasContents = "<font color=green><b>True</b></font>"
    73 End If
    74
    75
    76 v = TypeName(AI.Count)
    77 If Err > 0 Then
    78 Err.Clear
    79 Else
    80 hasCount = "<font color=blue><b>True</b></font>"
    81 End If
    82
    83 Response.Write("<td>item:"& hasItem&" | " &_
    84 "items:"& hasItems&" | " &_
    85 "contents:"& hasContents&" | " &_
    86 "count:"& hasCount&"</td></tr>" & vbNewLine)
    87
    88 Next
    89 Response.Write("</table>" & vbNewLine)
    90 %>

    输出结果:

    TypeName操作TypeName结果是否有Items,Item,Contents,Count属性
    ObjectContext   IASPObjectContext item:False | items:False | contents:False | count:False
    Server   IServer item:False | items:False | contents:False | count:False
    Application   IApplicationObject item:True | items:False | contents:True | count:False
    Application.Contents   IVariantDictionary item:True | items:False | contents:False | count:True
    Application.StaticObjects   IVariantDictionary item:True | items:False | contents:False | count:True
    Session   ISessionObject item:True | items:False | contents:True | count:False
    Session.Contents   IVariantDictionary item:True | items:False | contents:False | count:True
    Session.StaticObjects   IVariantDictionary item:True | items:False | contents:False | count:True
    Request   IRequest item:True | items:False | contents:False | count:False
    Response   IResponse item:False | items:False | contents:False | count:False
    Server.GetLastError   IASPError item:False | items:False | contents:False | count:False
    Request.ServerVariables   IRequestDictionary item:True | items:False | contents:False | count:True
    Request.ClientCertificate   IRequestDictionary item:True | items:False | contents:False | count:True
    Request.Cookies   IRequestDictionary item:True | items:False | contents:False | count:True
    Request.Form   IRequestDictionary item:True | items:False | contents:False | count:True
    Request.QueryString   IRequestDictionary item:True | items:False | contents:False | count:True
    Response   IResponse item:False | items:False | contents:False | count:False
    Response.Cookies   IRequestDictionary item:True | items:False | contents:False | count:True
    Request.Form("ID")   IStringList item:True | items:False | contents:False | count:True
    Request.QueryString("ID")   IStringList item:True | items:False | contents:False | count:True
    Request.Cookies("ID")   IReadCookie item:True | items:False | contents:False | count:True
    Request.ClientCertificate("Subject")   IRequestDictionary item:True | items:False | contents:False | count:True
    Request.ServerVariables("URL")   IStringList item:True | items:False | contents:False | count:True
    Server.CreateObject("ADODB.Connection")   Connection item:False | items:False | contents:False | count:False
    Server.CreateObject("ADODB.Recordset")   Recordset item:False | items:False | contents:False | count:False
    Server.CreateObject("Scripting.FileSystemObject")   FileSystemObject item:False | items:False | contents:False | count:False
    Server.CreateObject("Scripting.Dictionary")   Dictionary item:True | items:True | contents:False | count:True


     

    备注:

    1.对于Request.Form,Request.QueryString,类型为 IRequestDictionary,它们是集合对象,它(们)拥有Item属性及Count方法.

    对于Item,这里有参考部分:

    Item 属性

    Object.Item(natIndex) 

    参数

    Object
    EnumNetworkDrive 或 EnumPrinterConnections 方法的结果,或者由 Environment 或 SpecialFolders 属性返回的对象。
    natIndex
    要检索的项目。

    说明

    Item 是每个集合的默认属性。对于 EnumNetworkDrive 和 EnumPrinterConnections 集合来说,索引是一个整数;对 Environment 和 SpecialFolders 集合来说,索引是一个字符串。

    如果请求的文件夹 (strFolderName) 不可用,则 WshShell.SpecialFolders.Item (strFolderName) 在 VBScript 中返回 "Empty",而在 JScript 中返回 "undefined"。

    在这里,对于Request.Form(及Request.QueryString),当被用于输出,+-*/等操作时(TypeName,VarType等特殊类型操作除外),它相当于Request.Form.Item()(及Request.QueryString.Item()).它们相应返回接收到的参数字符串

    如果要获取查询参数当中有多少个参数,可以如下代码:

    <%
    Dim itemKey,itemVal
    For Each itemKey In Request.QueryString
    itemVal = Request.QueryString.Item(itemKey) '或:Request.QueryString(itemKey)
    Response.Write(itemKey & ": " & itemVal & "<br/>" & vbNewLine)
    Next
    %>

    2.对于Request.Form("id")(及Request.QueryString("id")),类型为 IStringList,它们也是集合对象,它(们)拥有Item属性及Count方法.

    如果要获取查询参数当中ID参数项,用以下代码是严重错误的:

    <%
    Dim itemKey,itemVal
    For Each itemKey In Request.QueryString.Item("id")
    itemVal = Request.QueryString.Item("id").Item(itemKey)
    '或:Request.QueryString("id")(itemKey)
    Response.Write(itemKey & ": " & itemVal & "<br/>" & vbNewLine)
    Next
    %>

    它不能正确输出各项.因为For each...Next语句对IRequestDictionary及IStringList的元素解析不一样,正确的代码为:

    <%
    Dim itemKey,itemVal,iItem
    For iItem = 1 To Request.QueryString.Item("id").Count
    itemKey = iItem
    itemVal = Request.QueryString.Item("id").Item(itemKey)
    '或:Request.QueryString("id")(itemKey)
    Response.Write(itemKey & ": " & itemVal & "<br/>" & vbNewLine)
    Next
    %>

    可见,Request.QueryString及Request.QueryString("id")在key-value方面有着差别.

    前者:以各个接收的参数名分别作为item的key名.用for each ... next可正确枚举

    后者:从1到count分别作为item的key名.用for each...next不可正确枚举(或不雅观),可用for ..to..next及count结合解决

    至此,item,count及返回类型方面的归结暂时停止...

    然后,对于item(),还有一些简写及其说明必须交代一下:

    以下代码执行效果一样:

    <%
    Response.Write "<br/>" & Request.QueryString
    Response.Write "<br/>" & Request.QueryString()
    Response.Write "<br/>" & Request.QueryString.Item
    Response.Write "<br/>" & Request.QueryString.Item()
    %>

    以下代码执行效果一样:

    <%
    Response.Write "<br/>" & Request.QueryString("id")
    Response.Write "<br/>" & Request.QueryString("id")()
    Response.Write "<br/>" & Request.QueryString("id").Item
    Response.Write "<br/>" & Request.QueryString("id").Item()
    Response.Write "<br/>" & Request.QueryString.Item("id")
    Response.Write "<br/>" & Request.QueryString.Item("id").Item
    Response.Write "<br/>" & Request.QueryString.Item("id").Item()
    %>

    以下代码执行效果一样:

    <%
    Response.Write "<br/>" & Request.QueryString.Count
    Response.Write "<br/>" & Request.QueryString().Count
    %>

    以下代码执行效果一样:

    <%
    Response.Write "<br/>" & Request.QueryString("id").Count
    Response.Write "<br/>" & Request.QueryString.Item("id").Count
    %>

    值得注意的地方:不要以下的错误的代码写法:

    <%
    Response.Write "<br/>" & Request.QueryString.Item.Count
    Response.Write "<br/>" & Request.QueryString.Item().Count

    Response.Write "<br/>" & Request.QueryString("id").Item.Count
    Response.Write "<br/>" & Request.QueryString.Item("id").Item().Count
    %>

    即item及item()之后不能接.Count,而item("id")或不用item时才能接.Count

    因为不管IRequestDictionary还是IStringList,其Item()的结果为字符串了,不能再进行count调用

    Request.QueryString.Item()其结果为所有的查询参数串成的字符串

    Request.QueryString.Item("id").Item()或Reuqest.QueryString("id").Item()其结果为所有的查询参数为ID的参数名值串成的字符串

    其它:Dictionary字典对象集合才有Items()方法,不要与item()或item(key)弄混.

    字典的items()方法将返回一个变量数组,以结合for .. to .. next和Ubound()操作

    字典同时还有item及count属性

    字典符合For each...next操作

    Application及Session当中还有类似Item的Contents属性,返回集合对象IVariantDictionary.操作性等 同于IRequestDictionary

    Application及Session当中还有Item属性,真的吗???(当然不是,是验证函数的问题!!)

    本文参考:

    http://msdn.microsoft.com/zh-cn/library/windows/apps/aa650188

    <<Windows脚本技术>> 

  • 相关阅读:
    信用卡:银联,VISA,MasterCard
    Syncthing vs BitTorrent Sync
    语言代码
    ATMEL精妙的IRQ中断处理过程
    CAN 总线通信控制芯片SJA1000 的读写
    ARM ® and Thumb ®-2 指令系统
    DeJaVu update history
    74系列的型号
    2007 Audi A4 INSTRUMENT CLUSTER WIRING DIAGRAM
    0-10岁儿童体重、身高参考值
  • 原文地址:https://www.cnblogs.com/dreamyoung/p/2432265.html
Copyright © 2020-2023  润新知