Use the. NET Component in ASP. NET

Source: Internet
Author: User

Many people are confused when @ Import and @ Assembly page identifiers. This article will solve these questions for you and tell you how to use. NET components. </P> <P> what is the difference between the class and ASP? </P> <P> if you have developed ASP programs before, you should be very familiar with the following code: </P> <script runat = "server" language = "VBScript">
Dim fso
Set fso = Server. CreateObject ("Scripting. FileSystemObject ")
</Script> </P> <P> the code above tells us that you need to use the CreateObject method of the Server object in ASP to create a class instance. In the above Code, the variable fso is declared first, and then the variable fso is assigned to the object of FileSystemObject (the FileSystemObject object can be found in the Scripting library ). If you want the above ASP code to run properly, you must ensure that the relevant DLL file has been installed and registered to the server. When the VBScript Runtime library is installed, the FileSystemObject object is automatically registered. However, if you want to use components developed by a third party or components created by yourself, you need to install and register your components carefully.
Next let's take a look at how to instantiate the object FileSystemObject in ASP. NET: </P> <P> using VB. NET:
<Script runat = "server" language = "VB">
Dim fso As Scripting. FileSystemObject = New Scripting. FileSystemObject ()
</Script> </P> <P> use C #:
<Script runat = "server" language = "c #">
Scripting. FileSystemObject fso = new Scripting. FileSystemObject ();
</Script> </P> <P>
As you can see, the instantiation class in ASP. NET is different from ASP. Important differences are: </P> <P> in ASP. NET, VBScript is no longer available, but replaced by powerful VB. NET.
In ASP. NET, When you declare a variable, you can specify its type and initialize the variable.
To reference a class, you must use the following representation: namespace [. subnamespace]. Class.
In the above example, we reference the FileSystemObject class, which can be found in the namespace Scripting. It should be noted that the above namespace is actually just a namespace we assume, it does not exist in the. NET architecture, so the above Code is actually not running. In other words, we need to create the Scripting namespace and define the FileSystemObject class in this namespace. </P> <P> what is a namespace? </P> <P> in the previous chapter, we used the word "namespace. With namespaces, you can combine multiple classes into logical units. Generally, you aggregate classes that provide similar functions or have similar states. For example, an instance of the namespace System. IO Contains classes used to process input and output operations (such as reading, writing, and deleting files. It is worth noting that the class in the namespace must have similar functions or similar states. You can organize your own namespaces according to any rules, or even without any rules. </P> <P> reference. NET Component </P> <P> note that the first ASP. the NET example is only used for teaching demonstration-it cannot run. Now let's look at a practical example that can run normally: </P> <P> using VB. NET to create a Message object
<% @ Assembly Name = "System. Messaging. dll" %>
<Script runat = "server" language = "VB">
Dim myDir As System. Messaging. Message = New System. Messaging. Message ()
</Script> </P> <P> use C # To create a Message object
<% @ Assembly Name = "System. Messaging. dll" %>
<Script runat = "server" language = "C #">
System. Messaging. Message myDir = new System. Messaging. Message ();
</Script> </P> <P> command @ Assembly indicates that a set is referenced to the current page, so that all the classes, interfaces, and structures defined in the set can be freely used on the current page. In the example given above, we bound the set System. Messaging. dll. The set contains the System. Messaging namespace. The System. Messaging namespace provides the ability to access. NET architecture messages. We have created an instance of the Message class so that we can use it to access messages in the Message queue. If we need to create a Message class in a file bound to the code, we need to do this: </P> <P> use VB. NET to create a Message object in the code binding file.
Public Class myPage
Inherits System. Web. UI. Page
Dim myDir As System. Messaging. Message = New System. Messaging. Message ()
End Class </P> <P> use C # To create a Message object in the code binding File
Public class myPage: System. Web. UI. Page {
System. Messaging. Message myDir = new System. Messaging. Message ();
} </P> <P> note that if we want to compile this class, we need to notify the compiler when compiling that we need to reference the System. messaging. dll and System. web. dll files. Suppose we save the above class as a file (mypage. vb or mypage. cs), and then we need to compile it like this:
Classes bound to the compilation Code </P> <P> vbc mypage. vb/r: System. Messaging. dll/r: System. Web. dll
Csc mypage. cs/r: System. messaging. dll/r: System. web. dll </P> <P> the parameter "/r: System. messaging. dll "and"/r: System. web. dll "the same command @ Assembly used on the Web Form plays the same role. </P> <P> what is a set? </P> <P> A set is a combination of logical units with similar functions. It is a basic, customizable Configuration unit with version control, reusable, and licensed security control attributes. It contains a list of all the specified version requirements, security identifiers, and other information. </P> <P> introduce the namespace </P> <P> we have noticed that whenever we reference the Message object, we all need to provide a complete namespace path (also called a fully qualified class name ). It is hard to imagine that after a long time, this will bring annoying and unnecessary bloated code. Fortunately, we can define a namespace path on a page to save unnecessary keyboard time. </P> <P> use VB. NET to introduce a set
<% @ Assembly Name = "System. Messaging. dll" %>
<% @ Import Namespace = "System. Messaging" %>
<Script runat = "server" language = "VB">
Dim myDir As Message = New Message ()
</Script> </P> <P> use C # To introduce a set
<% @ Assembly Name = "System. Messaging. dll" %>
<% @ Import Namespace = "System. Messaging" %>
<Script runat = "server" language = "C #">
Message myDir = new Message ();
</Script> </P> <P> the code binding class should be written as follows: </P> <P> use VB. NET introduces a set 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 </P> <P> use C # To introduce a set in the code binding File
Using System. Web. UI;
Using System. Messaging;
Public class myPage: Page {
Message myDir = new Message ();
} </P> <P> You can specify a namespace path through the command @ Import (you can also use Imports or using statements. In this way, when referencing a class, we do not need to use a fully qualified namespace path. Note: If the Message class exists in the System. Web. UI and System. Messaging namespaces, we must use a fully qualified class name. </P> <P> it is very important to know what the introduced namespace can do and what it cannot do. It not only saves the programmer's time to press the keyboard, but also makes the program code readable. You need to understand that the command to officially connect a namespace to the page is @ Assembly or compile with the parameter/r. </P> <P> using the configuration file automatic control to connect a set to a web form is not only in the @ Assembly command mode, a set can be automatically introduced to a page in an application. No command @ Assembly is required for such a set. We can use <assemblies> In the config. web configuration file to automatically introduce the set. </P> <P> automatically introduces a set in the configuration file.
<Configuration>
<Compilation>
<Assemblies>
<Add assembly = "System. Messaging"/>
<Add assembly = "*"/>
</Assemblies>
</Compilation>
</Configuration> </P> <P> the asterisk "*" notifies ASP. NET to automatically introduce each set in the private set buffer of the application. With the <add> element, you can introduce any namespaces within the application scope. </P> <P> what is the private set buffer of an application? </P> <P>. NET documentation defines the private set buffer of the application as follows: the private set buffer of the application is seen as a sub-directory in the application and. NET Architecture Installation Directory. However, in my test, only the in directory is treated as the private set buffer of the application. That is to say, only this directory is the scope of the asterisk. </P> <P> Note: The asterisk (*) in the <add> element is only automatically connected to the set in the application subdirectory!
Check the default configuration file config. web installed in the. NET architecture. We can see that it automatically introduces the following set: </P> <P> 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
* </P> <P> you can check the default config. web file by yourself.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.