public class CustomSection : ConfigurationSection
{
public CustomSecuritySection Security { get; private set; }
[ConfigurationProperty("type", IsRequired = true, DefaultValue = "QueryStringModule")]
public String type
{
get { return (String)base["type"]; }
set { base["type"] = value; }
}
[ConfigurationProperty("name", IsRequired = true, DefaultValue = "QueryStringModule")]
public String name
{
get { return (String)base["name"]; }
set { base["name"] = value; }
}
public CustomSection()
{
}
}
Configuration config = ConfigurationManager.OpenExeConfiguration(@"D:\xxxxx\xxxx\web.config");
//var httpmod = config.Sections.Add("TestSecton",
if (config.Sections["NewSection"] == null)
{
customSection = new CustomSection();
config.Sections.Add("NewSection", customSection);
config.Save(ConfigurationSaveMode.Full);
//ConfigurationManager.RefreshSection("NewSection");
}
當我用WebConfigurationManager.OpenWebconfiguration( 「/的web.config」)或( 「〜」)發生無效異常錯誤。它顯示無法映射路徑'/web.config'。錯誤信息。我也嘗試過使用xml,但它也不起作用。對我有沒有什麼好的解決方案。 – haripds
@haripds試試'WebConfigurationManager.OpenWebConfiguration(null)'([MSDN](http://msdn.microsoft.com/en-us/library/ms151456(v = vs.110).aspx)) – volpav
最後問題是解決了。非常感謝。 – haripds