C# asp.net mvc to create virtual directory

Use background:

virtual directory, a computer term, each Internet service can be published from multiple directories. Each directory can be located on a local drive or network by specifying directories with a universal naming convention (UNC) name, user name, and password for access rights. Designated customer URL Address,
The service submits the entire publication catalog set to the customer as a catalog tree. The host directory is the root of the virtual directory tree. Actual of virtual directory Subdirectory It is also available to customers. only http://www. The service supports virtual server; And FTP And gopher A service can only have one host directory.

The virtual directory does not appear in the directory list (also known as http://www. Directory browsing for the service). To access the virtual directory, the user must know the alias of the virtual directory and type in the browser URL To access.

To put it simply: in general, our file saving path is the default root directory. However, when you need to access data in other disks, you need to create a virtual directory to access it.

So how to create and use it? Don't talk much, open!

to configure:

1. Reference using system DirectoryServices;

2. On the web Config file < system Add nodes under Web >

          <identity impersonate="true" userName="Administrator" password="" />

Where userName is the computer user name and password is the computer login password. If not, fill in blank. You need to have this ID to operate the virtual directory of iis.

establish:

Add a new help class, and then write the following code (the remarks are very clear):

    

        /// <summary>
        /// Delete virtual path
        /// </summary>
        /// <param name="virtualdirname"></param>
        public static void DeleteVirtualDir(string virtualdirname)
        {
            DirectoryEntry _rootEntry;
            _rootEntry = new DirectoryEntry("IIS://localhost/W3SVC/" + SiteId + "/root");
            object[] paras = new object[2];
            paras[0] = "IIsVirtualDir";
            paras[1] = virtualdirname;
            _rootEntry.Invoke("Delete", paras);
            _rootEntry.CommitChanges();
        }

        /// <summary>
        /// Create virtual directory
        /// </summary>
        /// <param name="siteId">iis site ID</param>
        /// <param name="dirName">Virtual folder name</param>
        /// <param name="path">Physical path</param>
        /// <param name="userName">User name</param>
        /// <param name="userPass">User password</param>
        /// <param name="appPoolId">Application pool Id</param>
        /// <returns></returns>
        public static string CreateVirtualDir(string siteId, string dirName, string path, string userName, string userPass, string appPoolId)
        {
            string constIISWebSiteRoot = "IIS://localhost/W3SVC/" + siteId + "/ROOT";
            string virtualDirName = dirName;//Virtual directory name
            string physicalPath = path;
            try
            {
                DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
                foreach (System.DirectoryServices.DirectoryEntry v in root.Children)
                {
                    if (v.Name == dirName)
                    {

                        DeleteVirtualDir(dirName);
                    }
                }
                DirectoryEntry tbEntry = root.Children.Add(virtualDirName, "IIsWebVirtualDir");
                tbEntry.Invoke("AppCreate", true);
                //Set the physical path that the virtual directory points to
                tbEntry.Properties["Path"][0] = physicalPath;
                //Set read permissions
                tbEntry.Properties["AccessRead"][0] = true;
                //Allow write  
                tbEntry.Properties["AccessWrite"][0] = false;
                //Script resource access
                tbEntry.Properties["AccessExecute"][0] = false;
                //Allow anonymous access   
                tbEntry.Properties["AuthAnonymous"][0] = true;
                // Set the security of the directory. 0 means anonymous access is not allowed, 1 means allowed, 3 means basic authentication, and 7 means windows Inheritance authentication
                tbEntry.Properties["AuthFlags"][0] = 1;
                //Allow basic authentication 
                tbEntry.Properties["AuthBasic"][0] = false;
                //allow WIndows Integration verification   
                tbEntry.Properties["AuthNTLM"][0] = false;
                //Index this resource   
                tbEntry.Properties["ContentIndexed"][0] = false;
                //directory browse   
                tbEntry.Properties["EnableDirBrowsing"][0] = false;
                //Script executable   
                tbEntry.Properties["AccessScript"][0] = true;
                //Allow parent path   
                tbEntry.Properties["AspEnableParentPaths"][0] = true;
                //Application name
                tbEntry.Properties["AppFriendlyName"][0] = virtualDirName;
                //Application protection   
                tbEntry.Properties["AppIsolated"][0] = 2;
                //Set default document 
                //tbEntry.Properties["DefaultDoc"][0] = "index.asp,index.html,index.htm";
                tbEntry.Properties["EnableDefaultDoc"][0] = true;
                //Log access   
                tbEntry.Properties["DontLog"][0] = true;
                ////user name
                //tbEntry.Properties["AnonymousUserName"][0] = "Administrator";
                ////User password
                //tbEntry.Properties["AnonymousUserPass"][0] = "";
                ////Program pool
                //tbEntry.Properties["AppPoolId"][0] = appPoolId;
                tbEntry.CommitChanges();
                root.CommitChanges();
                return "1";
            }
            catch (Exception ex)
            {
                //return "0";
                return ex.Message + "<br>" + ex.Source;
            }
        }

use:

We call the CreateVirtualDir function when the project is running, but not all need to create a virtual directory. Here I set a parameter in the configuration file. Used to determine whether to open the virtual directory. Then, the configuration file also has the set physical path. This method is for reference only.

 

After iis is published and run, you can access it through the virtual path you created! As shown in the figure:

 

 

 

 

 

 

 

Conclusion:

It's almost over here. There is no particularly difficult operation, but it will be very convenient if it can be combined with the configuration file. It reduces cumbersome operations after publishing (no need for people to manually create virtual directories).

The above code has a method to get the name or path. Change it to the name you want.

Tags: ASP.NET

Posted by deezin on Thu, 06 Jan 2022 02:30:46 +1030