无法访问web.config的特定字段

时间:2018-07-12 09:56:07

标签: c# asp.net .net configuration

我在web.config文件中具有以下节点

<Properties>
  <Property Name="ConnectionString" Value="Data Source=testdb;User Id=scpan;password=testp@123;Persist Security Info=false"/>
</Properties>

我正在尝试使用System.Configuration.ConfigurationManager访问同一文件,但无法访问'ConnectionString'标签。怎么做?

我正在尝试下面的代码,但它也没有给我结果

var section = (XmlElement)ConfigurationManager.GetSection("Resource");
if (section.GetElementsByTagName("ConnectionString").Count > 0)
  {
    XmlElement element = (XmlElement)section.GetElementsByTagName("ConnectionString").Item(0);
    var fieldValue = element.Attributes["value"].Value;

   }

这是怎么了?

1 个答案:

答案 0 :(得分:0)

您需要像这样将连接字符串放在connectionStrings的{​​{1}}部分的configuration部分

web.config