Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Validate Configuration Options with ASP.NET Core

Validate Configuration Options with ASP.NET Core
November 20, 2022 3 min read BEN ABT
.NET ASP.NET Core

This article was written in 2022. Versions, tools and recommendations may have changed since then. Newer posts are collected under .NET.

Configuring an ASP.NET Core application can be done in a variety of ways, including using command-line arguments, environment variables and JSON files. One way to ensure that the options you have configured for your application are valid is to use the IOptions interface functionality, which provides a way to inject strongly-typed options into your application’s services.

Option validation is the process of verifying that the options used in an application have the correct values. This is important because options can be used to control various aspects of an application’s behavior, such as its performance, security and functionality. If an option has an incorrect value, it can cause the application to behave unexpectedly or even fail.

First, you need to define a class that will represent your configuration options. This class should have properties for each of the options you want to be able to set. For example:

C#
1public class MyConfig
2{
3    public string Option1 { get; set; }
4    public int Option2 { get; set; }
5    public bool IsOption3 { get; set; }
6}

Next, you need to register your configuration options with the Dependency Injection (DI) container. This is done in the ConfigureServices method of your Startup class, like so:

C#
1public void ConfigureServices(IServiceCollection services)
2{
3    services.Configure<MyConfig>(Configuration.GetSection("MyConfig"));
4}

Here, Configuration is an IConfiguration object that is provided by the Startup class and contains the application’s configuration settings. The GetSection method is used to retrieve the section of the configuration that contains the options for MyConfig.

Once your configuration options have been registered with the DI container, you can inject an IOptions<MyConfig> object into any of your application’s services. This object will contain the options that have been configured for MyConfig.

C#
1public class MyService
2{
3    private readonly MyConfig _config;
4
5    public MyService(IOptions<MyConfig> options)
6    {
7        _config = options.Value;
8    }
9}

You can then access the individual options of MyConfig through the Value property of the IOptions<MyConfig> object.

Using attributes

Validating your configuration options is a simple matter of adding [Required] and/or [Range] attributes to the properties of your MyConfig class. For example:

C#
 1public class MyConfig
 2{
 3    [Required]
 4    public string Option1 { get; set; }
 5
 6    [Range(1, 10)]
 7    public int Option2 { get; set; }
 8
 9    public bool Option3 { get; set; } = true;
10}

These attributes will cause an exception to be thrown if the corresponding options are not provided or if they are provided but do not satisfy the specified requirements.

You can use all attributes of the System.ComponentModel.DataAnnotations namespace.

Using PostConfigure

Another way is using the PostConfigure method to perform additional validation and/or initialization. This method takes a generic type parameter that specifies the type of options being configured and a lambda expression that receives those options as a parameter. For example:

C#
 1services.PostConfigure<MyConfig>(config =>
 2{
 3    if (string.IsNullOrEmpty(config.Option1))
 4    {
 5        throw new ArgumentException(
 6            $"{nameof(MyConfig.Option1)} of {nameof(MyConfig)} must be specified.");
 7    }
 8
 9    if (config.Option2 <= 0)
10    {
11        config.Option2 = 1;
12    }
13});

The advantage here is that even complex validation scenarios are covered and defaults can also be implemented more easily.

Likewise, custom execution types can be thrown at this point, which can be given their own section in Application Insights, for example, in order to design evaluations more easily.


In summary, using the IOptions functionality in ASP.NET Core allows you to easily inject strongly-typed configuration options into your application’s services and to validate those options to ensure that they are correct. This can help prevent errors and improve the reliability of your application.

Related articles

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026 - 12 min read

Problem Details in ASP.NET Core and .NET 11

builder.Services.AddProblemDetails() registers infrastructure. It does not create an error contract. An ASP.NET Core request can fail during …

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026 - 11 min read

Resilience in .NET with Microsoft.Extensions.Resilience

Retries are the most visible resilience strategy and the easiest one to misuse. Repeating a failing call can recover from a short network …

What Matters in ASP.NET Core 11

Sep 14, 2026 - 10 min read

What Matters in ASP.NET Core 11

ASP.NET Core 11 has no single feature that fundamentally changes how web applications are built. That is not a criticism. Mature frameworks …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026
Problem Details in ASP.NET Core and .NET 11

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026
Resilience in .NET with Microsoft.Extensions.Resilience

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026
What Matters in ASP.NET Core 11

What Matters in ASP.NET Core 11

Sep 14, 2026

Tags

.NET (192) Csharp (96) ASP.NET Core (48) Azure (41) DevOps (17) Docker (14) Microsoft (14) EF Core (11) Microsoft MVP (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Security (9) Synology (9) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7) Bicep (6)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact