簡體   English   中英

ASP.NET MVC配置另一個項目webconfig

[英]ASP.NET MVC Configure the another project webconfig

我想更改另一個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");

它說空參考

假設您有充分的理由這樣做,則可以使用以下方法:

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文檔對象模型(DOM)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM