How to read from appsettings in model class

  Kiến thức lập trình

I am returning an instance of this class from an API:

public class MerchantInfo
{
    public int MerchantId { get; set; }
    public string ClickUrl => $"{Environment.GetEnvironmentVariable("WebsiteDomain")}/{MerchantId}";
}

Appsettings.json:

{
    "WebsiteDomain": "SomeDomain.com"
}

Environment.GetEnvironmentVariable("WebsiteDomain") is returning null, so I’m wondering if I have to inject IConfiguration in the ctor in order to access appsettings.json values?

It feels wrong to be injection IConfiguration into a model class.

LEAVE A COMMENT