2017-05-23 187 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");