繁体   English   中英

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

[英]Not able to access specific field of web.config

我在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;

   }

怎么了

您需要像这样将连接字符串放在web.config configuration部分的connectionStrings部分中

<configuration>
    <!--other sections -->

    <connectionStrings>
        <add name="connstring" connectionString="your--connection--string" />
    </connectionStrings>

    <!--other sections -->
</configuration>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM