简体   繁体   中英

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.

Error: Unrecognized configuration path '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");

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. 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.

Aging question, but I thought I'd share my solution.

The location parameter must match the location path in your applicationHost.config file. This means that if websiteName is actually an application under another site, eg Default Web Site, its location section will look like this:

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

If so, your line 5 should look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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