简体   繁体   中英

IIS - How to edit local config for application with Microsoft.Web.Administration

I can create a website using ServerManager just fine using:

iisApplication = site.Applications.Add("/MySite", SomePath);

I want to edit some properties (but only specific to this application, not site or server-wide). Using this code

            Configuration config = iisApplication.GetWebConfiguration();

            ConfigurationSection defaultDocumentSection = config.GetSection("system.webServer/defaultDocument");
            ConfigurationElementCollection documents = defaultDocumentSection.GetCollection("files");
            documents.Clear();
            ConfigurationElement document = documents.CreateElement();
            document["value"] = "MyFile.aspx"; 
            documents.Add(document);

As soon as I commit changes I get the error

Filename: \\?\C:\inetpub\wwwroot\MySite\web.config
Error: Cannot write configuration file

Now it looks as though GetWebConfiguration() is pulling in the wrong web.config file because my site is not stored in inetpub, it is in a different folder.

How do I edit web.config in the local website folder? This is winforms.

I found the answer - it has to be edited at a higher level using the 'location' path:

config = iisManager.GetApplicationHostConfiguration();
ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Default Web Site/MySite);
anonymousAuthenticationSection["enabled"] = true;
iisManager.CommitChanges();

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