[英]How to add entries to IIS virtual directory's configuration
I'm trying to add some custom properties to the configuration of a virtual directory in IIS but when I'm committing the changes I get a DirectoryNotFoundException 我正在尝试为IIS中的虚拟目录配置添加一些自定义属性,但是当我提交更改时,我得到一个DirectoryNotFoundException
I've tried multiple ways of doing it, most something like this 我尝试了多种方法,大多数都是这样的
var config = serverManager.GetWebConfiguration(serverManager.Sites[0].Name, "mycustomfolder");
var appSettings = config .GetSection("appSettings");
var collection = section.GetCollection();
var elem = conf.CreateElement("add");
elem.SetAttributeValue("key", "createdBy");
elem.SetAttributeValue("value", "me");
serverManager.CommitChanges();
The server manager object is clearly defined with 服务器管理器对象已明确定义
using(var serverManager = new ServerManager())
...
What I was expecting would be something like this inside the configuration: 我期待的内容在配置中会是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
<appSettings>
<clear />
<add key="createdBy" value="me" />
</appSettings>
</configuration>
Instead I am getting an error 相反,我得到一个错误
System.IO.DirectoryNotFoundException: Filename: \\?\C:\inetpub\wwwroot\local\web.config
Error: Cannot write configuration file
at Microsoft.Web.Administration.Interop.IAppHostWritableAdminManager.CommitChanges()
at Microsoft.Web.Administration.ConfigurationManager.CommitChanges()
at Microsoft.Web.Administration.ServerManager.CommitChanges()
Following the path in which it is trying to save I cannot see subfolders for my virtual directories, but I can create them, even programmatically 按照它尝试保存的路径,我看不到我的虚拟目录的子文件夹,但我可以创建它们,甚至以编程方式
According to your description, I guess you may used the wrong site name. 根据你的描述,我猜你可能使用了错误的网站名称。 I have also created a test demo on my side, it works well. 我也在我身边创建了一个测试演示,它运行良好。 I suggest you could try to use site name directly. 我建议你可以尝试直接使用网站名称。
Details, you could refer to below codes: 详情,您可以参考以下代码:
protected void Button1_Click(object sender, EventArgs e)
{
using (var serverManager = new ServerManager())
{
var config = serverManager.GetWebConfiguration("WebForm48", "mycustomfolder");
var appSettings = config.GetSection("appSettings");
var collection = appSettings.GetCollection();
var elem = collection.CreateElement("add");
elem.SetAttributeValue("key", "createdBy");
elem.SetAttributeValue("value", "me");
collection.Add(elem);
serverManager.CommitChanges();
}
}
My IIS console: 我的IIS控制台:
Result: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.