【发布时间】:2013-01-28 11:59:12
【问题描述】:
我想在运行时使用 c# 将键值对添加到 Azure Web 角色 web.config 文件。
非常感谢任何帮助。
【问题讨论】:
标签: c# azure asp.net-mvc-4 web-config azure-web-roles
我想在运行时使用 c# 将键值对添加到 Azure Web 角色 web.config 文件。
非常感谢任何帮助。
【问题讨论】:
标签: c# azure asp.net-mvc-4 web-config azure-web-roles
这就是我们最终的做法。
我们在 web.config 中添加了一个具有空白值的 Key,然后在运行时为其添加了一个值。我认为可以修改相同的代码以在运行时添加键和值对。
using (var server = new ServerManager())
{
var siteNameFromServiceModel = "Web";
var siteName =
string.Format("{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
string configFilePath = server.Sites[siteName].Applications[0].VirtualDirectories[0].PhysicalPath + "\\Web.config";
XElement element = XElement.Load(configFilePath);
element.Element("appSettings").Elements("add").Where(X => X.Attribute("key").Value == "YourKeyName").Single().Attribute("value").Value = "YourValue";
element.Save(configFilePath);
}
【讨论】: