Many people are confused when @Import and @assemblying pages are identified, this article will unlock these questions to tell you how to use .NET components.
What changes have been compared to classes in the ASP?
If you have previously developed an ASP program, you should be very familiar with the following code:
DIM FSO
SET FSO = Server.createObject ("scripting.filesystemObject")
script>
The above code tells us to create a class instance using the CreateObject method of the Server object among the ASP. In the above code, first declare the variable FSO, then we have to assign the variable FSO to the object of FileSystemObject (FileSystemObject object can be found in the Scripting this library). If you want to run normally, we must determine that the associated DLL file has been installed and registered to the server. When the VBScript runtime is installed, the FileSystemObject object is automatically registered. But if you want to use the components developed by third parties or use the components you created yourself, you need to be careful to install and register your components.
Let's take a look, assuming that there is also a FILESYSTEMOBJECT object in ASP.NET, we should instantiate it:
Use VB.NET:
Dim fso as scripting.FileSystemObject = new scripting.filesystemObject ()
script>
Use C #:
Scripting.filesystemObject fso = new scripting.FileSystemObject ();
script>
As you can see, instantiate the class in ASP.NET is different from the ASP. The important differences are:
You can no longer use the VBScript language in ASP.NET, but instead of being strong VB.NET.
In ASP.NET, you can specify its type and initialize this variable while you declare a variable.
In order to reference a class, you need to use this representation: namespace [. Sub-namespace]. Class.
In the above example, we reference the FileSystemObject this class, and FileSystemObject This class can be found in the namespace scripting. It should be noted that the above namespace actually just a namespace we assume, it does not exist in the .NET architecture, so the above code is actually unable to run. In other words, we need to build this name space yourself and define the FileSystemObject this class in this name space.
What is the namespace?
In the previous chapter, we have used the word "namespace". With namespace you can consist of multiple classes to some of the units logically. Typically, you will add some similar features or classes with similar states. An example of the namespace system.io contains classes used to process input and output operations (such as: read, write, delete files). It is worth noting that the class in the namespace has a similar function or a similar state is not a required requirement. You can organize your own namespace in accordance with any rules, and you can even have any rules. Quote .NET components
It should be noted that the first ASP.NET we give is only used to teach demo - it is unable to run. Let us now look at an actual example of work properly:
Built a Message object using VB.NET
<% @ AskEMBLY Name = "System.Messaging.dll"%>
Dim mydir as system.Messaging.Message = new system.Messaging.Message ()
script>
Use a C # to establish a Message object
<% @ AskEMBLY Name = "System.Messaging.dll"%>
System.Messaging.Message MyDir = new system.Messaging.Message ();
script>
The instruction @Assembly indicates that a collection references a collection to the current page, allowing all the classes, interfaces, interfaces defined in the collection, can be used freely in the current page. In the example given above, we bind the collection of System.Messaging.dll. In this collection contains System.Messaging this namespace, System.Messaging namespace provides the ability to access .NET architecture messages. We have established an instance of the Message class so that you can use it to access the message in the message queue. If we need to create a Message class in a file binding file, you need this:
Create a Message object in the code binding file using VB.NET
Public class mypage
Inherits System.Web.ui.page
Dim mydir as system.Messaging.Message = new system.Messaging.Message ()
END CLASS
Built a Message object in a code binding file using C #
Public class mypage: system.Web.ui.page {
System.Messaging.Message MyDir = new system.Messaging.Message ();
}
It should be noted that if we want to compile this class, we need to notify the compiler when compiling. We need to reference the system.Messaging.dll and the system.web.dll two files. Suppose we save the above class becomes a file (myPage.vb or mypage.cs), then we need to compile it:
Compile code binding class
VBC mypage.vb /r:System.Messaging.dll /R :system.web.dll
Csc mypage.cs /r :system.messaging.dll /r:System.web.dll Parameters "/R :System.Messaging.dll" and "/R :system.meb.dll" in the same place The role of instruction @assembily used on web form is the same.
What is a collection?
A collection is a combination of logic units with similar functions. He is the foundation, the configuration unit that can be described, has version control, reuse, and licensing attributes. It contains a list of collections used to express all specified versions, security monodes, and other information.
Introduced namespace
We have noticed that whenever we reference the Message object, we need to provide a full namespace path (also known as a fully qualified class name). It's not difficult to imagine, after the time is long, this will bring trouble and unnecessary bloated code. Fortunately, we can define a path to a namespace to save some of the unnecessary time of knocking keyboard.
Introducing a collection using VB.NET
<% @ AskEMBLY Name = "System.Messaging.dll"%>
<% @ Import namespace = "system.Messaging"%>
DIM mydir as message = new message ()
script>
Use C # to introduce a collection
<% @ AskEMBLY Name = "System.Messaging.dll"%>
<% @ Import namespace = "system.Messaging"%>
Message mydir = new message ();
script>
This should be written in our code binding class:
Use VB.NET to introduce a collection in the code binding file
Imports System.Web.ui
Imports system.Messaging
Public class mypage
Inherits Page
DIM mydir as message = new message ()
END CLASS
Introduce a collection in the code binding file using C #
Using system.Web.ui;
Using system.Messaging;
Public class mypage: Page {
Message mydir = new message ();
}
With instruction @Import (same can be passed by imports or using statements), we can specify a path to a namespace. In this way, when we reference a class, we don't have to use a fully qualified namespace path. Note: If you have Message this classes in both namespaces in System.Web.ui and System.Messaging, we must use a fully qualified class name.
It is very important to know what the introduced namespace can do and what can't be done is very important. It is not only the time saving the programmer's tap keyboard, but more importantly, it brings good readability of the program code. It should be understood that the command that truly encompasses the namespace into the page is: @assembly or compiling parameters / r. Automatic control by profile This collection can be used to a web form is not only using the @assembly instruction, and the collection can also be automatically introduced into a page in an application. Such a collection does not require instruction @assembly. We can use
Automatic introduction set in the configuration file
askSEMBLIES>
compilation>
configure>
This asterisk "*" notifies that ASP.NET automatically introduces every collection in the "Private Collection Buffer" of the application. With
What is the private collection buffer of the app?
The private set buffer of the .NET is the private collection buffer of the application: the application's private collection buffer is considered to be a subdirectory / bin and the installation directory of the .NET architecture. However, through my test, only the / bin directory is used as a private collection buffer for the application, that is, only this directory is the scope of the asterisk "*".
Note: The asterisk "*" in the
The default profile installed by consult the .NET architecture: config.web. We can know that he automatically introduces the following collection:
Mscorlib
SYSTEM
System.data
System.Diagnostics
SYSTEM.DRAWING
SYSTEM.NET
System.Text.RegularExpressions
System.Web
System.Web.Services
SYSTEM.XML
System.xml.Serialization
Microsoft.comServices
*
You can take a look at the default config.web file, you will confirm. The following namespace is also automatically introduced:
Microsoft.visualBasic
SYSTEM
System.collections
System.text
System.Text.RegularExpressions
System.Web
System.Web.caching
System.Web.SessionState
System.web.security
System.Web.ui
System.Web.ui.WebControls
System.Web.ui.htmlControls