Recently, I plan to try to implement MVC architecture in ASP. Someone must have asked me: ASP has been eliminated, why are I still studying it? I also know this. Since Microsoft abandoned ASP 3.0 and switched to ASP.NET, ASP has lagged far behind PHP and JSP that started almost at the same time as it. The benefits of open source over closed source are just like PHP and ASP. ASP says it will be eliminated and no one can save it. However, it is worth noting that ASP is quite wide in the Chinese market, especially some applications of some small and medium-sized enterprises. Simple CMS is no problem and it is simple to deploy. On some old Windows systems, it does not require the installation of .NET Framework to run directly, so it is still necessary to prepare a framework. However, this is an experimental framework, which is just to verify whether ASP can implement a MVC architecture similar to PHP.
Okay, after saying so much, let’s just turn to the topic. The reason for this problem is that I need to dynamically include ASP files. As you know, there is only one include method in ASP, that is SSI (Server Side Include), which is basically divided into the following two types:
The code copy is as follows:
<!-- #include file="sample.asp" -->
<!-- #include virtual="sample.asp" -->
Basically, the first one for these two types is used more. #include virtual contains virtual paths, which can be used in virtual directories. But both of these are static. If we want to include dynamically, we cannot write it as:
The code copy is as follows:
<!-- #include file="<%=MyVar%>" -->
<!-- #include virtual="<%=MyVar%>" -->
The above writing method is incorrect. It can be understood that the #include directive is executed before the ASP starts the script engine to execute the ASP<% %> tag. In other words, #include is not the work of ASP, but the server-side program, such as the translation work of IIS, so you will not pay attention to your ASP code.
How to implement include, include_once, require, and require_once dynamically include script methods similar to PHP? Let’s take a look at a method of ASP Server object: Server.Execute. Search for all ASP features and you can find that this function is most similar to dynamic include. We can do an experiment:
Sample.inc.asp
The code copy is as follows:
<%
Response.Write "Hello World!"
%>
test.asp
The code copy is as follows:
<%
Server.Execute "Sample.inc.asp"
Response.Write "I am test.asp!"
%>
The actual output should be "Hello World!I am test.asp!", indicating that Server.Execute works well in dynamic inclusion, but what if I want to include classes or functions? Next, do the following experiment:
Sample.class.asp
The code copy is as follows:
<%
Class Sample
End Class
%>
test.asp
The code copy is as follows:
<%
Server.Execute "Sample.class.asp"
Response.Write TypeName(Eval("New Sample"))
%>
Run directly, the error "Microsoft VBScript runtime error Error '800a01fa' class is not defined: 'Sample'", the result is very disappointing, why does this happen? I looked up MSDN and found this description: "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." It seems to be a bit different from the problem I encountered. Is Server.Execute code-isolated? Then perform the following experiment: