ASP.NET Core startup validation part 4
Introduction Learn how to use a class that implements IValidateOptions to validate that sections exist with the required keys in the appsettings.json file. Source code Source code requires Microsof...

Source: DEV Community
Introduction Learn how to use a class that implements IValidateOptions to validate that sections exist with the required keys in the appsettings.json file. Source code Source code requires Microsoft Visual Studion 2022 or higher. appsettings.json { "Logging": { "LogLevel": { "Microsoft.EntityFrameworkCore.Database.Command": "Information", "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "Helpdesk": { "Phone": "123-456-7890", "Email": "[email protected]" } } In the file, we want to validate that both Logging and HelpDesk are validated. Required classes 💡 properties must be nullable for validation code to work. public class LogLevelSettings { public string? Default { get; set; } = string.Empty; [ConfigurationKeyName("Microsoft.AspNetCore")] public string? MicrosoftAspNetCore { get; set; } = string.Empty; [ConfigurationKeyName("Microsoft.EntityFrameworkCore.Database.Command")] public string? MicrosoftEntityFrameworkCoreDatabaseCommand { get; set; }