A_L
A_L

Reputation: 1146

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.

Upvotes: 0

Views: 1031

Answers (2)

A_L
A_L

Reputation: 1146

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

Upvotes: 1

Nemi Chand
Nemi Chand

Reputation: 146

please check this link

Creating a new website programmatically on IIS using ASP.NET and C#

Hope it helps you.

Upvotes: 0

Related Questions