IIS-如何使用Microsoft.Web.Administration编辑应用程序的本地配置

本文关键字:应用程序 配置 编辑 Administration 何使用 Microsoft Web IIS- | 更新日期: 2023-09-27 18:00:25

我可以使用ServerManager创建一个网站,只需使用:

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

我想编辑一些属性(但仅限于此应用程序,而非站点或服务器范围)。使用此代码

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

一旦我提交更改,我就会收到错误

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

现在看起来GetWebConfiguration()拉入了错误的web.config文件,因为我的网站没有存储在inetpub中,而是在另一个文件夹中。

如何编辑本地网站文件夹中的web.config?这是winforms。

IIS-如何使用Microsoft.Web.Administration编辑应用程序的本地配置

我找到了答案-必须使用"location"路径在更高级别进行编辑:

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

请检查此链接

使用ASP.NET和C#在IIS上以编程方式创建新网站

希望它能帮助你。