【问题标题】:ASP.NET MVC Configure the another project webconfigASP.NET MVC 配置另一个项目 webconfig
【发布时间】:2019-09-18 14:50:49
【问题描述】:

我想更改另一个 Webconfig,但我遇到了错误

我尝试的是

string path = "E:\\username\\myprojects\\myproject\\Web.config";
Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(path);
webConfig.AppSettings.Settings.Add("DBNAME", "DEV_TEMP");
webConfig.Save();

这表示“Web.config 拒绝了相对虚拟路径”

另一个我在 webconfig 上尝试过

 <configuration>
   <appSettings>
     <add key="DBNAME" value="DEV_DEVELOPMENT"/>
   </appSettings>

DBNAME 值更改为“DEV_TEMP”

XDocument doc=XDocument.Load("E:\\username\\myprojects\\myproject\\Web.config");

doc.Element("appSettings").Element("DBNAME").Value = "DEV_TEMP";
doc.Save("Web.config");

它说空引用

【问题讨论】:

  • 在尝试编辑配置文件之前关闭 VS。

标签: c# xml asp.net-mvc


【解决方案1】:

假设您有充分的理由这样做,您可以使用以下内容:

var addElement = doc.Element("configuration").Element("appSettings").Elements().First(x => x.Attribute("key").Value == "DBNAME");
var valueAttribute = addElement.Attribute("value");
valueAttribute.Value = "DEV_TEMP";

请注意,您还需要获取configuration 元素,并且您可以通过选择key 属性与DBNAME 匹配的元素来获取add 元素,因为可能存在多个add 元素。 value 也是 add 的属性,而不是元素本身。请参阅XML Document Object Model (DOM) 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-27
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 2011-01-08
    • 1970-01-01
    相关资源
    最近更新 更多