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