ASP 真正实现包含文件中用变量

作者:vkvi 来源:ITPOW(原创) 日期:2009-2-10

传统的动态调用包含文件有以下几种方法:

用 FSO 去读指定的文件,然后输出。

缺点:复杂;且只能输出,不能执行文件中的 ASP 代码。

条件判断输出哪个包含文件。

<%if f = 1 then%>
<!--#include file="file1.asp"-->
<%elseif f = 2 then%>
<!--#include file="file2.asp"-->
<%elseif f = 3 then%>
<!--#include file="file3.asp"-->
<%end if%>

缺点:如果条件很多,将导致程序过于复杂。

本文介绍用 Server.Execute。

<%
dim filePath = "/file1.asp"
Server.Execute(filePath)
%>

filePath 的值可以被动态改变,这样就实现了包含文件中用变量。

  • 被包含文件 filePath 是文件路径,不能是 "/file1.asp?id=1"。但 filePath 中的代码可以使用 Request.QueryString 来获取 GET 给包含文件的数据。
  • 被包含文件 filePath 中的代码可以使用 Request.Form 来获取 POST 给包含包含文件的数据。
  • 被包含文件 filePath 与包含文件中代码的变量是隔绝的。

相关阅读

相关文章