ASP动态包含文件问题
突然有个动态包含文件的问题,想实现如 <!--#include file= " <%=filename%> "--> 的功能,后来才发现这样不行,因为 <!--#include优于一切ASP脚本代码,所以它直接就把 <%=filename%> 当了文件名。
后来用网上提供的include函数来包含文件,解决了这一问题,但所包含的文件里定义的类却不能实例化,提示类不存在,不知道这是什么原因,请高手指点:
例:
index.asp
<!--#include file= "include.asp "-->
<%
include( "a.asp ")
Set objA = new A '这里不能实例化,郁闷
%>
a.asp
<%
Class A
Public p
End Class
%>
include.asp
<%
Function include(filename) '动态包含文件函数
Dim re,content,fso,f,aspStart,aspEnd
Set fso=CreateObject( "Scripting.FileSystemObject ")
Set f=fso.OpenTextFile(server.mappath(filename))
content=f.ReadAll
f.close
Set f=nothing
Set fso=nothing
Set re=new RegExp
re.Pattern= "^\s*= "
aspEnd=1
aspStart=inStr(aspEnd,content, " <% ")+2
Do While aspStart> aspEnd+1
Response.write Mid(content,aspEnd,aspStart-aspEnd-2)
aspEnd=inStr(aspStart,content, "%\> ")+2
Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2), "Response.Write "))
aspStart=inStr(aspEnd,content, " <% ")+2
Loop
Response.write Mid(content,aspEnd)
Set re=nothing
End Function
%>
[解决办法]
要用ExecuteGlobal
重写过的include.asp
<%
Sub include(filename) '动态包含文件函数
Dim re,content,fso,f,aspStart,aspEnd
Set fso=CreateObject( "Scripting.FileSystemObject ")
Set f=fso.OpenTextFile(server.mappath(filename))
content=f.ReadAll
f.close
Set f=nothing
Set fso=nothing
Set re=new RegExp
re.Pattern= "^\s*= "
aspEnd=1
aspStart=inStr(aspEnd,content, " <% ")+2
Dim s
s = " "
Do While aspStart> aspEnd+1
s = s & Mid(content,aspEnd,aspStart-aspEnd-2)
aspEnd=inStr(aspStart,content, "%\> ")+2
s = s & re.replace(Mid(content,aspStart,aspEnd-aspStart-2), "Response.Write ")
aspStart=inStr(aspEnd,content, " <% ")+2
Loop
s = s & Mid(content,aspEnd)
Set re=nothing
ExecuteGlobal s
End Sub
%>