How to operate the virtual directory principle analysis and implementation scheme of IIS in Net

  • 2020-05-19 04:35:12
  • OfStack

.Net has actually done a good job for us in this respect. FCL provides a number of classes to help us do this, making our development work very simple and enjoyable. Programming IIS is actually very simple, like ASP1.Net requires ADSI to operate IIS, but at this point we don't need GetObject anymore, because Net gives us something new and more powerful.

The System.DirectoryServices namespace includes a number of powerful things --DirectoryEntry, DirectoryEntries, DirectoryEntry, DirectoryEntries, DirectoryEntry, DirectoryEntries, IIS, LDAP, NDS, and WinNT -- that give us access to the active directory
But we're only talking about IIS controls here, and generally speaking, we operate on IIS1 mostly on virtual directories, so I'll make that the main thing.

First of all, we need to understand the hierarchical structure of IIS. The following is a picture I found from abroad, which well explains the hierarchical structure of IIS:
To understand the control syntax of IIS, we must understand the diagram above and understand the hierarchy of IIS metadata (Metabase). Each node in the figure is called Key, and each Key can contain one or more values. These values are what we call attributes (properties). Key in IIS metadata matches elements in IIS, so the setting of attribute values in metadata will affect the setting of IIS. This is the basic idea and core of our programming.

Another thing to learn about is the concept of Schema. It represents the name of the architecture in IIS, meaning that it can understand the type of Key in IIS metadata, specifically the type of each node. As we know, IIS has virtual directories, normal directories, and files, and these are all elements of IIS, which are marked Schema. For example, the Schema of a virtual directory is "IIsVirtualDir", and the normal directory is "IIsWebDir". This way, when we add and remove directories, IIS knows whether we are adding a virtual directory or a normal directory.

Create a virtual directory
DirectoryEntry is a big gift from.Net, whose name we know what it does -- it's a directory entry. Anyone who has used ADSI knows how to operate IIS, WinNT and so on. We also need to provide their Path. When operating IIS, the Path format is:
 
IIS : //ComputerName/Service/Website/Directory 

ComputerName: the name of the operating server, which can be either IP or localhost
Service: the operating server, Web, FTP and SMTP are included in IIS. We mainly operate Web of IIS, so it is "W3SVC" here, and "MSFTPSVC" if it is FTP.
WebSite: a single IIS service can include many sites, which are used to set up the operation site. Its value is 1 number, the default is 1, the default site, if there are other, starting from 1 and so on.
Directory: needless to say, that is, the name of the operation directory, 1 site 1 top-level directory is "ROOT", the other directory is his children (Child).

First we get the top-level directory (root directory) of a site:
 
DirectoryEntry rootfolder = new DirectoryEntry (" IIS : //localhost/W3SVC/1/ROOT ");  

If we create the object without exception, then the directory is real.
Now let's add a new virtual directory, like "Aspcn" :
 
DirectoryEntry newVirDir = rootfolder.Children.Add (" Aspcn "," IIsWebVirtualDir ");  
newVirDir.Invoke (" AppCreate ", true );  
newVirDir.CommitChanges (a);  
rootfolder.CommitChanges (a);  

The idea of creating a directory is simple: add another record to the subset of the root directory (rootfolder.Children). The Add method in the DirectoryEntries class is used here. It returns an DirectoryEntry, representing the newly added directory. Then, using the Invoke method of DirectoryEntry, call the "AppCreate" method of ADSI to actually create the directory (it seems that you can create the directory successfully without this step, but to be on the safe side, you should use it), and finally, call the CommitChanges method of the new and root directories in turn to confirm this operation.

When creating a new directory, we can also assign values to the attributes of that directory, but my practical experience tells me that it is best not to do so. If we assign values at the time of creation, there will be many attributes that will not be assigned successfully, such as the important Path attribute representing the real directory. Therefore, it is recommended that you had better create a directory, and then assign a value, that is, update the directory information.

Update virtual directory
I believe you are familiar with IIS, and know some important Settings of IIS, such as readable (AccessRead), writable (AccessWrite), executable (AccessExecute), etc. These can all be achieved by assigning the Properties property set of DirectoryEntry. Assignment can be done in two ways:
The first is to call the Add method of the Properties collection, such as:
 
dir.Properties[ " AccessRead " ].Add ( true );  

The second is to assign a value to the first index:
 
dir.Properties[ " AccessRead " ][0] = true ;  

Both of these methods are feasible. It depends on what you like.
We use the Find method of the DirectoryEntries class, such as:
 
DirectoryEntry de = rootfolder.Children.Find (" Aspcn "," IIsVirtualDir ");  

Once we find it, we can assign it. Assign a value when 1 must take a good look at ah, the attribute value of virtual directory can be too much, 1 check 1 pile :(too much, flying knife I also don't repeat, we go to the Microsoft site to check :)
More commonly used are: AccessRead, AccessWrite, AccessExecute, AccessScript, DefaultDoc, EnableDefaultDoc, Path

Delete virtual directory
The easy way to delete virtual directories is to find the virtual directory you want to delete and call the AppDelete method.
 
DirectoryEntry de = rootfolder.Children.Find (" Aspcn "," IIsVirtualDir ");  
de.Invoke (" AppDelete ", true );  
rootfolder.CommitChanges (a);  

Another method is to call the Delete method of the Root directory.
 
object[] paras = new object[2] ;  
paras[0] =  " IIsWebVirtualDir ";  // Indicates that the virtual directory is being manipulated  
paras[1] =  " Aspcn ";  
rootfolder.Invoke (" Delete ", paras );  
rootfolder.CommitChanges (a);  

Related articles: