If you're having conceptual difficulties with the @ Import and @ Assembly directives, you're not the only one. Read this article to find out about the use of .NET components in ASP.NET and forget you ever had doubts.
What Has Changed Compared to ASP Classic?
If you've developed Active Server Pages you will be familiar with the following notation:
Components in ASP Classic
<script runat="server" language="VBScript">
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
</script>
To create an instance of a class we use the CreateObject method of the Server object. In the above code the variable fso is first declared and then assigned a reference to the FileSystemObject object that can be found in the Scripting library. For this to work it is necessary that the corresponding DLL is installed and registered on the server. The registration of the FileSystemObject object occurred automatically when the VBScript runtime was installed. If you want to use a 3rd-party component or one you created yourself, you will need to take care of installing and registering it.
Let's now have a look at how the FileSystemObject object, if it existed, would be instantiated in ASP.NET:
ASP.NET Equivalent (VB.NET)
<script runat="server" language="VB">
Dim fso As Scripting.FileSystemObject = New Scripting.FileSystemObject()
</script>
ASP.NET Equivalent (C#)
<script runat="server" language="c#">
Scripting.FileSystemObject fso = new Scripting.FileSystemObject();
</script>
As you can see, there are some differences to be found. The most important are:
VBScript has been dropped and replaced by the fully-fledged VB.NET (a.k.a. VB 7.0).
While declaring a variable you can specify its type and initialize it.
To refer to a class, the notation Namespace[.SubNamespace].Class notation is used. In the above example we are referring to the FileSystemObject class that can be found in the Scripting namespace. Note that such namespace doesn't exist in the .NET Framework and therefore the above code will not work. On the other hand we could create our own Scripting namespace and define the FileSystemObject class in it.
What is a Namespace?
In the previous section the word "namespace" has been used. By means of namespaces you can systematize classes into logically related units. As a rule, you will cluster classes that provide similar functionality or have a similar context. For instance the System.IO namespace contains classes that are used to deal with Input-Output operations like reading, writing or deleting files. Note that similar functionality and/or similar context is not a formal requirement. You are free to organize your namespaces along any rules, even no rules.
Referring to .NET Components
As already noted, the first ASP.NET example was there for didactical reasons--it wouldn't work. Let's now have a look at a working example:
Creating the Message object (VB.NET)
<%@ Assembly Name="System.Messaging.dll" %>
<script runat="server" language="VB">
Dim myDir As System.Messaging.Message = New System.Messaging.Message()
</script>
Creating the Message object (C#)
<%@ Assembly Name="System.Messaging.dll" %>
<script runat="server" language="C#">
System.Messaging.Message myDir = new System.Messaging.Message();
</script>
The @ Assembly directive links an assembly to the current page, making all of the classes, interfaces and structures defined in the assembly available for use. In our example we are binding the System.Messaging.dll assembly. This assembly