简体   繁体   English

IIS错误:无法识别的配置路径'MACHINE / WEBROOT / APPHOST / websiteName

[英]IIS Error: Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/websiteName

I am trying to set IP Address and Domain Restrictions in the c# code, i am following this article, but it gives me unrecognized location error. 我想设置IP地址,并在C#代码域限制 ,我下面这个文章,但它给了我无法识别的位置误差。

Error: Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/websiteName 错误:无法识别的配置路径'MACHINE / WEBROOT / APPHOST / websiteName

My Code: 我的代码:

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();

            }

It gives me error after this line: 在这一行之后它给了我错误:

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

Can any one tell what is wrong, or something that i have missed. 任何人都可以说出错误,或者我错过了什么。

You might get a broken applicationHost.config for IIS/IIS Express, where the web site fails to have a root application. 您可能会在IIS / IIS Express中获得一个损坏的applicationHost.config,其中该网站无法拥有根应用程序。 If you don't know how to edit that, post the <sites> tag of your file in the question so that others can review and suggest how to fix. 如果您不知道如何编辑,请在问题中发布文件的<sites>标记,以便其他人可以查看并建议如何修复。

Aging question, but I thought I'd share my solution. 老化的问题,但我想我会分享我的解决方案。

The location parameter must match the location path in your applicationHost.config file. location参数必须与applicationHost.config文件中的位置路径匹配。 This means that if websiteName is actually an application under another site, eg Default Web Site, its location section will look like this: 这意味着如果websiteName实际上是另一个站点下的应用程序,例如默认网站,其位置部分将如下所示:

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

If so, your line 5 should look like this: 如果是这样,你的第5行应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM