【问题标题】:Struggling to read value from web.config file using ConfigurationManager.AppSettings["key"] in ASP .NET在 ASP .NET 中使用 ConfigurationManager.AppSettings["key"] 努力从 web.config 文件中读取值
【发布时间】:2021-10-03 22:50:46
【问题描述】:

正在努力使用 ConfigurationManager.AppSettings["key"] 从 web.config 文件中读取值。

使用 ASp .Net v5 和 C#

WEB.config 文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v5.0" />
  </startup>
  <appSettings>
    <add key="dbconnectionstring" value="mongodb://sdfgsdfgsdfgsdfg"/>
    <add key="Databasename" value="WineSales"/>
  </appSettings>
</configuration>

请求和读取代码:

[HttpPost("customer/addCustomer")]
    public IActionResult AddCustomer([FromBody] DTO_IN_Customer customerData)
{

    var yeet = ConfigurationManager.AppSettings["dbconnectionstring"];

    System.Console.WriteLine(yeet); 

    var result = _salesPersonService.AddCustomer(customerData);

    return Ok(result);
}

我是否缺少某些东西,因为我搜索的所有地方都是这样做的,当我在 var 结果处放置断点时,yeet 为空。

请帮忙!

【问题讨论】:

    标签: c# asp.net web-config


    【解决方案1】:

    将 web.config 文件重命名为 app.config,然后它应该可以工作了

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 2013-12-15
      • 2012-10-13
      • 1970-01-01
      • 2017-07-26
      • 2015-11-27
      • 2012-09-21
      • 2015-05-11
      • 1970-01-01
      相关资源
      最近更新 更多