Startup Validation in ASP.NET Core Part 3

Guide to validating critical paths and files at ASP.NET Core startup with ValidateOnStart and IValidateOptions using a reusable library. Centralize configuration, load keys per file, and expose the Help Desk phone and email in the interface.

domingo, 17 de agosto de 2025 • 3 min read • Q2BSTUDIO Team

Artificial-Intelligence-

Introduction

In this article titled ASP .NET Core startup validation part 3 we explain how to use ValidateOnStart to ensure that required directories exist using a reusable class library that avoids repeating code in multiple projects.

Objective

The objective is to centralize the validation of critical paths and files, for example folders where secrets such as the help desk service phone and email are stored, using IValidateOptions and ValidateOnStart so that validation runs at application startup.

Step 1

Add the class project with the validation classes to the front end project. Add a FileSettings section in appsettings.json with the SecretsDirectory value pointing to the folder containing the phone and email files. Example of a simplified section for reference: { Logging: { LogLevel: { Default: Information, Microsoft.AspNetCore: Warning } }, AllowedHosts: *, FileSettings: { SecretsDirectory: C:/Secrets } } The FileSettings.SecretsDirectory value must point to the Secrets folder of the HelpDeskApplication project where the phone and email files are located.

Step 2

Add the reference to the SpecialValidatorsLibrary project. To distribute the library among multiple projects, it is recommended to create a local NuGet package with SpecialValidatorsLibrary and use it from the consuming projects.

Step 3

In Program cs, bind the configuration section to the options class and register the validation so it runs at startup. The general flow is: register RequiredDirectories options and bind them to the FileSettings section; register the IValidateOptions implementation for RequiredDirectories, for example DirectoryOptionsValidation; call ValidateOnStart on the options to force the check at startup time; read FileSettings from the configuration to obtain the secretsPath; if the folder exists, add AddKeyPerFile from that path and configure the HelpDesk options with the loaded configuration.

In practical terms, this allows that when the application starts, it validates that the secrets path exists and that, if it exists, keys are loaded per file and help desk values are exposed through strongly typed options.

Step 4

In the layout view of the front end project, inject the HelpDesk options to display the phone and email in the footer. Replace the footer so it shows HelpDeskOptions.Value.Phone and HelpDeskOptions.Value.Email values and use mailto for the email link. This way the interface displays the real help desk information without hardcoding values.

Step 5

Run the front end project and verify that the footer contains phone and email. If startup validation fails because the path does not exist, the application will stop or log the error depending on the validation implementation, preventing it from being deployed in an inconsistent state.

Summary

Although the example uses a single folder to read values, this pattern is applicable to other folder validations needed for tasks such as file downloads, batch processing, or services that depend on external paths. Centralizing validation in a reusable library improves consistency and reduces errors.

About Q2BSTUDIO

At Q2BSTUDIO we are a custom software and application development company specialized in custom software solutions, artificial intelligence and AI for businesses, cybersecurity, AWS and Azure cloud services, business intelligence services, and Power BI consulting. We design custom AI agents, tailored artificial intelligence solutions, and scalable applications to meet specific business needs. Our approach integrates security practices and cloud deployment to deliver robust products aligned with each client's objectives.

Keywords and positioning

Custom applications, custom software, artificial intelligence, cybersecurity, AWS and Azure cloud services, business intelligence services, AI for businesses, AI agents, Power BI. These capabilities allow Q2BSTUDIO to offer complete solutions that include development, AI integration, data analysis, and protection of production environments.

Interesting links

More reference articles on startup validation: https://dev.to/karenpayneoregon/aspnet-core-startup-validation-20e7 and https://dev.to/karenpayneoregon/aspnet-core-startup-validation-part-2-3f7m

If you need help implementing startup validations, integrating strongly typed options, or deploying secure applications in the cloud, contact Q2BSTUDIO for custom solutions in software development, artificial intelligence, and cybersecurity.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.