Using custom .net classes in ASP Classic Compiler

To use custom .net classes, I extended VBScript syntax with the Imports statement. The Imports statement introduce the top namespace to the VBScript, as in the example below. The following example demonstrates using ADO.NET in ASP Classic Compiler:

<%
    imports system
    dim filePath = Server.MapPath("/Database/authors.mdb")
    dim oleConn = new system.data.oledb.oledbconnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filePath)
    oleConn.Open()

    dim cmd = new system.data.oledb.OleDbCommand("SELECT * From authors", oleConn)
    dim dr = cmd.ExecuteReader()
    do while dr.Read()
        response.Write(dr.GetString(1) & "<br/>")
    loop
%>

In the example above, “imports system” instructs VBScript.net that system is a namespace rather than an object. When VBScript.net encounters system.data.oledb.oledbconnection, it follows the namespace hierarchy to find the oledbconnection class.

However, VBScript.net does not automatically search all the loaded assemblies. Instead, it only searches the assemblies it was instructed search. VBScript.net will always search the system.dll assembly. However, to instruct VBScript.net to also search in the system.data.dll, we need to add the following code to global.asax.cs:

using System.Reflection;
using Dlrsoft.Asp;

protected void Application_Start(object sender, EventArgs e)
{
    Assembly a1 = Assembly.Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
    AspHandlerConfiguration.Assemblies.Add(a1);
}

The above code tells AspHandler to search the System.Data.dll assembly for classes.

Lastly, to saves the typing of the fully qualified class name, the “imports” statement also supports alias. The following code demonstrates using alias to save typing:

<%
imports t = system.text

dim sb = new t.StringBuilder()
sb.append("this")
sb.append(" is ")
sb.append(" stringbuilder!")
response.write sb.toString()

%>

No Comments