Back to articles
ASP.NET Core startup validation part 4

ASP.NET Core startup validation part 4

via Dev.toKaren Payne

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" : "helpdesk@example.com" } } 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 ? MicrosoftEntityFramework

Continue reading on Dev.to

Opens in a new tab

Read Full Article
6 views

Related Articles