There is a simple approach to read values from appsettings.json file in AspNet Core. We write our custom configuration on appsetting.json file available on the root folder of the project.
Why do we need custom configuration?
In an application, there are some constants that need to be used throughout the application. Hard-coding them whenever and wherever we need it is always a bad practice on application development.
So, maintaining all these constants from the single place helps to create a maintainable software solution.
Sending an email is the basic feature which takes some parameters like hostname, port-no, password, company-email etc which doesn’t change frequently.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"mailConfig": { | |
"mailFrom": "info@coreprogramm.com", | |
"host": "smtp.google.com", | |
"portNo": "465", | |
"password": "CorePa$$word" | |
} | |
} |
First Way
In ConfigureServices() method, we register the configuration with singleton instead of Configure() method.
In ConfigureServices() method, we register the configuration with singleton instead of Configure() method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddSingleton(Configuration.GetSection("mailConfig").Get<MailConfig>()); | |
services.AddControllersWithViews(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MailConfig | |
{ | |
public string mailFrom { get; set; } | |
public string host { get; set; } | |
public string password { get; set; } | |
public int portNo { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MailController : Controller | |
{ | |
private readonly MailConfig _config; | |
public MailController(MailConfig config) | |
{ | |
_config = config; | |
} | |
public IActionResult Index() | |
{ | |
string companyEmail = _config.mailFrom; | |
string hostName = _config.host; | |
string password = _config.password; | |
int portNo = _config.portNo; | |
return View(); | |
} | |
} |
Second Way
Register the configuration on ConfigureServices() method of Startup.cs class. The appsettings.json may consist many sections based on the requirement of the project. Notice that “mailConfig” on GetSection() method represents the section name.
Now we inject the IOptions<T> on the constructor of the Homecontroller.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller | |
{ | |
private readonly ILogger<HomeController> _logger; | |
private readonly IOptions<MailConfig> _config; | |
public HomeController(ILogger<HomeController> logger, IOptions<MailConfig> config) | |
{ | |
_logger = logger; | |
_config = config; | |
} | |
public IActionResult Index() | |
{ | |
string companyEmail = _config.Value.mailFrom; | |
string hostName = _config.Value.host; | |
string password = _config.Value.password; | |
int portNo = _config.Value.portNo; | |
return View(); | |
} | |
} |
![]() |
You can see all the mail configs are read from appsettings.json |