2017-05-23 149 views
4

我想設置IP地址和域限制在C#代碼,我下面this文章,但它給了我無法識別的位置誤差。IIS錯誤:無法識別的配置路徑「MACHINE/WEBROOT/APPHOST/websiteName

Error: Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/websiteName

我的代碼:

using (var serverManager = new ServerManager()) 
      { 
       var config = serverManager.GetApplicationHostConfiguration(); 
       var ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "websiteName"); 
       var ipSecurityCollection = ipSecuritySection.GetCollection(); 

       var addElement = ipSecurityCollection.CreateElement("add"); 
       addElement["ipAddress"] = @"SomeIP"; 
       addElement["allowed"] = false; 
       ipSecurityCollection.Add(addElement); 

       var addElement1 = ipSecurityCollection.CreateElement("add"); 
       addElement1["ipAddress"] = @"SomeIP"; 
       addElement1["subnetMask"] = @"255.255.0.0"; 
       addElement1["allowed"] = false; 
       ipSecurityCollection.Add(addElement1); 

       serverManager.CommitChanges(); 

      } 

它給了我這個錯誤行之後:

var ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "websiteName");

任何一個可以告訴什麼是錯,什麼是我錯過了。

+0

您是否在運行具有管理員權限的Visual Studio? –

+0

@PankajKapare:我曾嘗試使用管理員privilages運行visual studio,但它給出了相同的錯誤。 –

+0

在第3行中,您使用的是「erverManager」而不是serverManager。我相信它的錯字。 –

回答

0

對於IIS/IIS Express而言,您可能會遇到破壞的applicationHost.config,該網站未能擁有根應用程序。如果您不知道如何編輯該文件,請在問題中發佈文件的<sites>標籤,以便其他人可以查看和建議如何解決。

0

老化的問題,但我想我會分享我的解決方案。

location參數必須與applicationHost.config文件中的位置路徑匹配。這意味着如果websiteName實際上是另一個站點下的應用程序,例如默認Web站點,它的位置部分將是這樣的:

<location path="Default Web Site/websiteName"> 
    <system.webServer> 
     <.../> 
    </system.webServer> 
</location> 

如果是這樣,你的5號線應該是這樣的:

var ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "Default Web Site/websiteName");