- ASP.NET Core MVC
- ASP.NET Core logging with Serilog
- ASP.NET Core managed config
- ASP.NET Core MVC with /api/-area
- ASP.NET Core API versioning
- ASP.NET Core API with Swagger UI
- ASP.NET Core EF in separate project
- ASP.NET Core AutoMapper
- ASP.NET Core response cache
- ASP.NET Core memory cache
- ASP.NET Core TypeScript
Reading config is pretty straight forward task that most applications need. For many years I used Nini, which scratched that itch fairly well. With the advent of better libraries its not too difficult to deserialize your favorite formatted config file into a class.
With .Net Core we finally got some decent/easy to use config system built in by the help of the Options pattern. In short, reading config by deserializing sections of appsettings.json into DI-hosted objects.
Install
Install-Package Microsoft.Extensions.Options.ConfigurationExtensions
Implement
In Startup.cs ConfigureServices method:
services.Configure<AppSettings>(Configuration);
services.Configure<OneSectionSettings>(Configuration.GetSection("TheSection"));
This would require a couple of classes, for example:
public class AppSettings
{
public string RootValue { get; set; }
public OneSectionSettings TheSection { get; set; }
}
public class OneSectionSettings
{
public string SomeValue { get; set; }
}
And a corresponding config section
"RootValue": "Yes, yes",
"OneSection": {
"SomeValue": "Test"
}
If config section/values do not exist it will not produce any error.
Using
public class HomeController : Controller
{
private AppSettings _appSettings;
public HomeController(IOptions<AppSettings> appSettings)
{
_appSettings = appSettings.Value;
}
...
}
Recall config object by IOptions<object> and use its “Value” property to extract object.