If [Exists] Request("FieldName") Then ... End If
解决方法
If Request("FieldName") <> "" Then 'etc. End If
我通常使用以下代码之一的某些变体显式检查Form和QueryString集合,如果我可能从一个或另一个获取变量,具体取决于上下文:
Select Case True Case Request.Form("FieldName") <> "" 'Run if the Form isn't empty Case Request.QueryString("FieldName") <> "" 'Run if the QueryString isn't empty Case Else 'Set a predefined default if they're both empty End Select
或嵌套的If …然后:
If Request.Form("FieldName") <> "" Then 'Run if the Form isn't empty ElseIf Request.QueryString("FieldName") <> "" Then 'Run if the QueryString isn't empty Else 'Set a predefined default if they're both empty End If
如果我确切地知道它来自哪个集合,我将专门检查该集合.原因是我想确保它从我期望它来自的地方拉出我所期望的.当我不期待它时,我不希望有人通过在QueryString中发送内容来覆盖Form值.
从MSDN开始:
If the specified variable is not in one of the preceding five
collections,the Request object returns EMPTY.All variables can be accessed directly by calling Request(variable)
without the collection name. In this case,the Web server searches the
collections in the following order:
- QueryString
- Form
- Cookies
- ClientCertificate
- ServerVariables
If a variable with the same name exists in more than one collection,
the Request object returns the first instance that the object
encounters.It is strongly recommended that when referring to members of a collection the full name be used. For example,rather than Request.(“AUTH_USER”) use Request.ServerVariables(“AUTH_USER”). This allows the server to locate the item more quickly.