【问题标题】:Not able to access specific field of web.config无法访问 web.config 的特定字段
【发布时间】:2018-12-20 12:03:58
【问题描述】:

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

   }

这里有什么问题?

【问题讨论】:

  • @mjwills 我更新了我的帖子

标签: c# asp.net .net configuration


【解决方案1】:

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

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

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

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

【讨论】:

  • 我能问一下为什么吗?
  • 它是一个旧文件,并且仅在提到的选项卡下添加了连接字符串
  • 如果它是一个旧文件,那么到目前为止你是如何读取它的?
  • 我尝试使用另一个使用 XML 阅读器但无法阅读。我已经发布了代码。请看看
  • 将连接字符串放在配置文件中的 connectionStrings 部分是一种做法。
猜你喜欢
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2021-06-28
  • 1970-01-01
  • 2012-04-16
  • 2021-03-04
  • 1970-01-01
相关资源
最近更新 更多