如果我设置
@ENABLESESSIONSTATE = false
然后
session("foo") = "bar"
那么结果就是
Microsoft VBScript运行时错误"800a0114">
变量未定义"会话">
。。。文件和行无
这通常表明对程序流的错误假设,我会跟踪并解决这个问题。
然而,在一组特定的情况下,我会遇到这样一种情况:在每个页面请求中,总是首先调用一段使用会话的代码。这与性能监控有关。
这段代码包括一个fork——如果用户有一个会话,我们就走一条路,如果没有,我们就去另一条路。
当然,当用户会话由于我们引入了一些在禁用会话的情况下运行的代码而不存在时,我们就会崩溃。
我可以用解决
on error resume next
session("foo") = "bar"
if err.number <> 0 then
' do the no-has-session fork
else
' do the has-session fork
end if
on error goto 0
但我想知道是否还有一种不那么棘手的方法。
为了让这个问题显示一个可接受的答案。。。。
关于使用isObject((方法的建议,结果并不好。以下asp。。。
<%@EnableSessionState=False%>
<% option explicit
response.write "session enabled=" & IsObject(Session)
response.end
%>
中的结果
Microsoft VBScript运行时错误"800a01f4">
变量未定义:"会话">
/errortest.asp,第6行
因此,会话对象似乎被标记为真正未声明。
我的建议是构造一个函数,如下所示。
<%@EnableSessionState=False%>
<% option explicit
response.write "session enabled=" & isSessionEnabled() ' <-- returns false
response.end
function isSessionEnabled()
dim s
isSessionEnabled = true ' Assume we will exit as true - override in test
err.clear() ' Clear the err setting down
on error resume next ' Prepare to error
s = session("foobar") ' if session exists this will result as err.number = 0
if err.number <> 0 then
on error goto 0 ' reset the error object behaviour
isSessionEnabled = false ' indicate fail - session does not exist.
exit function ' Leave now, our work is done
end if
on error goto 0 ' reset the error object behaviour
end function ' Returns true if get to this point
%>
然后将其用作
If isSessionEnabled() then
' do something with session
else
' don't be messin with session.
end if