I'm trying to get value from appsettings to MyController in .Net Core 3.1 MVC. Here I need to achieve this in below way:
private readonly IConfiguration _config;
public MyController(IConfiguration config)
{
_config = config;
}
public string apiURL = _config.GetValue<string>("ServiceURL");
But when I try to do so, I'm getting below error.
A field initializer cannot reference the non-static field, method, or property 'MyController._config'
CodePudding user response:
Change your field to be a property instead, like this:
public string apiURL => _config.GetValue<string>("ServiceURL");
When using =
you are creating a field.
When using =>
you are creating a get-only property.