C
C#•3y ago
big OOF

Call public method

Hello, Im trying to create a "config" which retrieves values from the appsettings.json file. Ive created an example below:
public class ConfigModel : PageModel
{
private IConfiguration _configuration;
public ConfigModel(IConfiguration iconfig)
{
_configuration = iconfig;
}

public string UserName
{
get
{
return _configuration.GetValue<string>("Data:UserName");
}
set { }
}
public class ConfigModel : PageModel
{
private IConfiguration _configuration;
public ConfigModel(IConfiguration iconfig)
{
_configuration = iconfig;
}

public string UserName
{
get
{
return _configuration.GetValue<string>("Data:UserName");
}
set { }
}
How would you retrieve "UserName" from a different class? Something like?
public class TestClass
{
private static IConfiguration _configuration;
public static ConfigModel Config = new ConfigModel(_configuration);
public void ExampleLogin()
{
var a = Config.UserName;
var b = Config.Password;


}
}
public class TestClass
{
private static IConfiguration _configuration;
public static ConfigModel Config = new ConfigModel(_configuration);
public void ExampleLogin()
{
var a = Config.UserName;
var b = Config.Password;


}
}
Or what would you say is best practice? Thanks in advance! 🙂
1 Reply
Angius
Angius•3y ago
Well, yes, it's a property, so .UserName is correct I'd just make this property get-only
Want results from more Discord servers?
Add your server