Search code examples
c#asp.netweb-config

Struggling to read value from web.config file using ConfigurationManager.AppSettings["key"] in ASP .NET


Struggling to read value from web.config file using ConfigurationManager.AppSettings["key"].

Using ASp .Net v5 and C#

WEb.config file:

enter image description here

<?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>

Request and reading code:

[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);
}

Is there somethign I am missing, because everywhere I searched they are doing it this way and when I put a breakpoint at var result then yeet is empty.

Please help!


Solution

  • Rename the web.config file to app.config and then it should work