How to read Appsettings.Json in .net 6 Web API with F#

Viewed 20

I want to read a simple connection string from the appsettings.json file in F#

  "ConnectionStrings": {
    "myConnectionString": "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"
  }

In C# I could use the read the connection string with an object from a class implementing the Iconfiguration provided in the constructor. But in the F# web API with .net 6, I couldn't find anything that made me read the connection string section or an other key in from the AppSettings.json file

Here is an example how I could read from the Appsettings.json in C# .net 6 Web API. This is what I want to do.

public class TestModel : PageModel
{
    // requires using Microsoft.Extensions.Configuration;
    private readonly IConfiguration _configuration;

    public TestModel(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void Connect()
    {
      var connectionString = _config.GetConnectionString("myConnectionString");
      // Do Something with the string
    }
}
0 Answers
Related