1. 程式人生 > >利用ASP.NET操作IIS (可以制作安裝程序)

利用ASP.NET操作IIS (可以制作安裝程序)

isp ring 路徑 詳細 arp 可能 操作 nis rest

很多web安裝程序都會在IIS裏添加應用程序或者應用程序池,早期用ASP.NET操作IIS非常困難,不過,從7.0開始,微軟提供了 Microsoft.Web.Administration 類,可以很容易操作IIS。

本文主要介紹四點:

一.添加應用程序

二.添加應用程序池

三.設置應用程序所使用的應用程序池

四.IIS裏其他屬性的設置

首先,必須確保電腦上已經安裝了IIS,安裝後,系統默認會註冊一個DLL,通常位置是

C:\Windows\assembly\GAC_MSIL\Microsoft.Web.Administration\7.0.0.0__31bf3856ad364e35\Microsoft.Web.Administration.dll

要獲取站點和應用程序,代碼為:

 public static ApplicationPool getAppPool()
        {
            ServerManager manager = new ServerManager();
            return manager.ApplicationPools;
        }
 
        public static SiteCollection GetAllSite()
        {
            ServerManager manager = new ServerManager();
            SiteCollection sites = manager.Sites;
            return sites;
        }

  

一.添加應用程序

手動添加應用程序,通常是在“Default Web Site”上,右鍵,選擇“添加應用程序...”,然後在彈出的對話框裏,輸入應用程序名稱和物理路徑,如下圖所示。

技術分享 技術分享

這個操作,使用C#代碼操作為

   public static void  CreateVdir(string vdir, string phydir)
        { 
                ServerManager serverManager = new ServerManager();
                Site mySite = serverManager.Sites["Default Web Site"];
                mySite.Applications.Add("/" + vdir, phydir);
serverManager.CommitChanges(); }

首先定義一個ServerManager 對象,然後用Sites獲取站點的集合。,最後調用Applications屬性裏的Add方法將應用程序添加到站點裏。但是請註意:建立應用程序是以“/”為路徑的,所以,上面代碼裏傳入的參數,直接輸入應用程序名稱即可。

要在Default Web Site默認站點下建立一個名稱為 test的應用程序,其物理路徑為 D:\web\sample ,則調用為

CreateVdir("test","d:\\web\\sample")

  

刪除一個應用程序也很簡單,直接調用Remove方法即可。

    public static void DeleteVdir(string vDirName)
        {
            
                ServerManager serverManager = new ServerManager();
                Site mySite = serverManager.Sites["Default Web Site"];
                Microsoft.Web.Administration.Application application = mySite.Applications["/" + vDirName];
                mySite.Applications.Remove(application);
                serverManager.CommitChanges(); 
        }

  

二、添加應用程序池

手動在IIS裏添加應用程序池為直接在IIS控制面板裏添加。

技術分享技術分享

使用c#代碼如下

   public static void CreateAppPool( string appPoolName)
        {
 
                ServerManager serverManager = new ServerManager();
                serverManager.ApplicationPools.Add(appPoolName);
                ApplicationPool apppool = serverManager.ApplicationPools[appPoolName];
                apppool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
                apppool.Enable32BitAppOnWin64 = true;
                apppool.ManagedRuntimeVersion = "v4.0";
                serverManager.CommitChanges();
                apppool.Recycle();
            
        }

從上面代碼裏可以看到,ApplicationPool 提供了 Enable32BitAppOnWin64 、ManagedPipelineMode 、ManagedRuntimeVersion等屬性,可以很容易使用。

就像上面代碼,要使用集成模式可以直接設置 apppool.ManagedPipelineMode = ManagedPipelineMode.Integrated 。

  

刪除應用程序池同樣也比較簡單,調用remove方法

   public static void DeleteAppPool(string appPoolName)
        {
             
                ServerManager serverManager = new ServerManager();
                ApplicationPool apppool = serverManager.ApplicationPools[appPoolName];
                serverManager.ApplicationPools.Remove(apppool);
                serverManager.CommitChanges();
       }
           

  

三:設置應用程序所使用的應用程序池

手動操作:在IIS裏,選擇應用程序後,在高級設置裏,可以直接修改應用程序所使用的應用程序池。

技術分享

要完成類似上面的操作,可以設置Applications的ApplicationPoolName屬性,該屬性表示應用程序所使用的應用程序池名稱。

     public   static void AssignVDirToAppPool(string vdir, string appPoolName)
        {
 
                ServerManager serverManager = new ServerManager();
                Site site = serverManager.Sites["Default Web Site"];
                site.Applications["/" + vdir].ApplicationPoolName = appPoolName;
                serverManager.CommitChanges();
            
        }

  

四:控制IIS裏的其他屬性

除了應用程序和應用程序池,使用ServerManager類還可以操控IIS裏的更多屬性。例如設置ISAPI和CGI的限制。

手動操作為:打開ISAPI和CGI操作,在需要的.NET版本上啟用或者禁用。

技術分享技術分享

完成上面操的代碼如下

  

            using (ServerManager serverManager = new ServerManager())
              {
                  Configuration config = serverManager.GetApplicationHostConfiguration();
                  ConfigurationSection isapiCgiRestrictionSection = config.GetSection("system.webServer/security/isapiCgiRestriction");
                  ConfigurationElementCollection isapiCgiRestrictionCollection = isapiCgiRestrictionSection.GetCollection();

                  foreach (ConfigurationElement element in isapiCgiRestrictionCollection)
                  {
                      string path = element["path"].ToString();
                      string allowed = element["allowed"].ToString();
                      string description = element["description"].ToString();
                      string groupId = element["groupId"].ToString();


                      if (description == "ASP.NET v4.0.30319")
                      {
                        
                            element["allowed"] = true;
                        
                      }


                  }

                  serverManager.CommitChanges();
              }

  

你甚至可以在ISPAPI裏增加自己的配置:

                ConfigurationElement addElement = isapiCgiRestrictionCollection.CreateElement("add");
                addElement["path"] = @"C:\Windows\abc\aspnet_isapi.dll";
                addElement["allowed"] = true;
                addElement["groupId"] = @"ContosoGroup";
                addElement["description"] = @"Contoso Extension";
                isapiCgiRestrictionCollection.Add(addElement);
                serverManager.CommitChanges();

當然,上面代碼裏的“難點”是,可能你並不知道IIS裏,各個屬性的 Section("system.webServer/security/isapiCgiRestriction") 是多少,不過不用擔心

微軟的官方文檔已經詳細列出了各個節點,你只要根據根據需要使用相應的節點即可進行相關操作。

網址為: https://docs.microsoft.com/en-us/iis/configuration/system.webserver/security/isapicgirestriction/

技術分享

有了上面的屬性,如果後期想定制exe版本的web安裝程序,就可以使用這些類控制IIS。  

利用ASP.NET操作IIS (可以制作安裝程序)