Microsoft.Extensions.Options.DataAnnotations 9.0.0-preview.2.24128.5
About
Microsoft.Extensions.Options.DataAnnotations is a library that adds extra validation functionality to configuration options using data annotations.
It allows to apply validation rules to configuration classes to ensure they are correctly configured before the application starts running.
This way, misconfiguration issues are catched early during the application startup rather than facing them later in production.
Key Features
- Enables validation of configuration options using data annotations.
- Early detection of misconfiguration issues during application startup.
How to Use
While configuring services, chain the ValidateDataAnnotations()
and ValidateOnStart()
methods to the AddOptions
method for your configuration class.
Here is a simple example demonstrating how to validate options on application startup:
services
.AddOptions<MyOptions>()
.ValidateDataAnnotations()
.ValidateOnStart();
In the configuration class, use data annotations to specify the validation rules.
For instance, in the following MyOptions
class, the Name
property is marked as required:
using System.ComponentModel.DataAnnotations;
public class MyOptions
{
[Required(AllowEmptyStrings = false)]
public string Name { get; set; }
}
With this setup, an error indicating that the Name
field is required will be thrown upon startup if it hasn't been configured.
Main Types
The main types provided by this library are:
Microsoft.Extensions.Options.DataAnnotationsValidateOptions<TOptions>
Microsoft.Extensions.DependencyInjection.OptionsBuilderDataAnnotationsExtensions
Additional Documentation
Related Packages
Core options: Microsoft.Extensions.Options
Feedback & Contributing
Microsoft.Extensions.Options.DataAnnotations is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.
Showing the top 20 packages that depend on Microsoft.Extensions.Options.DataAnnotations.
Packages | Downloads |
---|---|
Microsoft.AspNetCore.App
Provides a default set of APIs for building an ASP.NET Core application.
This package requires the ASP.NET Core runtime. This runtime is installed by the .NET Core SDK, or can be acquired separately using installers available at https://aka.ms/dotnet-download.
|
10 |
Microsoft.AspNetCore.App
Provides a default set of APIs for building an ASP.NET Core application.
This package requires the ASP.NET Core runtime. This runtime is installed by the .NET Core SDK, or can be acquired separately using installers available at https://aka.ms/dotnet-download.
|
5 |
Microsoft.AspNetCore.App
Provides a default set of APIs for building an ASP.NET Core application.
This package requires the ASP.NET Core runtime. This runtime is installed by the .NET Core SDK, or can be acquired separately using installers available at https://aka.ms/dotnet-download.
This package was built from the source at:
https://github.com/aspnet/Universe/tree/1016eae4004686f7fdad13f5c329f63bbae1f3a1
|
5 |
Microsoft.AspNetCore.App
Provides a default set of APIs for building an ASP.NET Core application.
This package requires the ASP.NET Core runtime. This runtime is installed by the .NET Core SDK, or can be acquired separately using installers available at https://aka.ms/dotnet-download.
|
4 |
Microsoft.AspNetCore.App
Provides a default set of APIs for building an ASP.NET Core application.
This package requires the ASP.NET Core runtime. This runtime is installed by the .NET Core SDK, or can be acquired separately using installers available at https://aka.ms/dotnet-download.
This package was built from the source at:
https://github.com/aspnet/Universe/tree/244e037a222a7cd0a17feafb7f3a625fde40de97
|
4 |
.NET Framework 4.6.2
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0-preview.2.24128.5)
- Microsoft.Extensions.Options (>= 9.0.0-preview.2.24128.5)
.NET 8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0-preview.2.24128.5)
- Microsoft.Extensions.Options (>= 9.0.0-preview.2.24128.5)
.NET 9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0-preview.2.24128.5)
- Microsoft.Extensions.Options (>= 9.0.0-preview.2.24128.5)
.NET Standard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0-preview.2.24128.5)
- Microsoft.Extensions.Options (>= 9.0.0-preview.2.24128.5)
- System.ComponentModel.Annotations (>= 5.0.0)
.NET Standard 2.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0-preview.2.24128.5)
- Microsoft.Extensions.Options (>= 9.0.0-preview.2.24128.5)
- System.ComponentModel.Annotations (>= 5.0.0)