直接运行,出现错误“Microsoft VBScript 运行时错误 错误 '800a01fa' 类没有被定义: 'Sample'”,结果很令人失望,为什么会出现这种情况呢?查阅了MSDN,找到这段描述:“If a file is included in the calling page by using #include, the executed .asp will not use it. For example, you may have a subroutine in a file that is included in your calling page, but the executed .asp will not recognize the subroutine name. ” 貌似和我遇到的问题有些不一样,难道Server.Execute是代码隔离的?再进行下面这个实验: Sample.inc.asp
<% Dim MyVar MyVar = "I am Sample!" %>
test.asp
<% Dim MyVar MyVar = "I am test!" Server.Execute "Sample.inc.asp" Response.Write MyVar %>
结果输出的是“I am test!”,很是失望!看来Server.Execute是变量、函数、类这类代码隔离的,也就是说调用端和被调用端在代码级别上互不干扰,看来Server.Execute只能用于包含.asp模板了。 下面隆重出场的是VBScript的脚本特性Execute,传给Execute的必须是有效的VBScript脚本代码,而且Execute是上下文相关的,这点看来很接近于我们需要的动态include。 test.asp
Function file_get_contents(filename) Dim fso, f Set fso = Server.CreateObject("Scripting.FilesystemObject") Set f = fso.OpenTextFile(Server.MapPath(filename), 1) file_get_contents = f.ReadAll f.Close Set f = Nothing Set fso = Nothing End Function Function class_get_contents(filename) Dim contents contents = file_get_contents(filename) contents = Replace(contents, "<" & "%", "") contents = Replace(contents, "%" & ">", "") class_get_contents = contents End Function