Accessing Settings During Setup in ASP.NET Core / .NET 8
ASP.NET Core is a cross-platform, open-source web framework by Microsoft that allows developers to build modern web apps and cloud-native applications efficiently. With the latest version of .NET, ASP.NET Core continues to provide a lightweight, secure, and flexible platform for developing web applications that can run on Windows, Linux, and macOS.
In this article, we’re going to take a deeper look at how to access configuration settings during setup in ASP.NET Core, following Tim Corey’s 10-minute training video. Tim demonstrates how developers can pull configuration values in program.cs before dependency injection is fully configured, which is an essential technique for creating flexible and maintainable web apps.
Why Early Access to Configuration Matters
Tim Corey starts by highlighting the importance of app settings in ASP.NET Core. According to Tim, appsettings.json is a “great place to store configuration” because it allows developers to change values without recompiling or redeploying applications.
In web development, it’s common to rely on configuration for services, such as setting URLs for APIs or adjusting environment-specific parameters. Normally, in a .NET Core web app, developers access configuration through dependency injection:
@inject IConfiguration configHowever, Tim points out that sometimes you need access to these configuration values during setup, for instance, when configuring HTTP clients or other services in program.cs. Doing so ensures that your applications remain flexible and adaptable as they move across different environments like development, staging, or production.
Setting Up a Minimal Blazor App
To demonstrate, Tim creates a simple Blazor application, turning off interactivity to focus entirely on program.cs. He emphasizes that the architecture of ASP.NET Core allows you to separate configuration from code, making it easier to manage web apps and cloud-native projects.
This approach also aligns with the cross-platform nature of the .NET platform, as the same configuration logic works on Windows, Linux, and macOS without modification.
Configuring an HTTP Client Using App Settings
Tim walks us through a scenario where an HTTP client is registered in program.cs:
builder.Services.AddHttpClient("LocalAPI", client =>
{
client.BaseAddress = new Uri("https://localhost:7654");
});While this works, Tim explains that hardcoding the URL is not ideal. If the API endpoint changes, developers must update the code and create a pull request for deployment. Instead, he recommends storing the value in appsettings.json:
{
"LocalAPI": "https://localapi.fromconfig"
}This ensures that the framework pulls the configuration dynamically, avoiding recompilation and reducing errors during deployment. It also aligns with best practices for secure and maintainable web apps.
Accessing Configuration in Program.cs
Tim demonstrates how to pull configuration values before dependency injection:
var localApiBaseAddress = builder.Configuration.GetValue<string>("LocalAPI");Key points Tim highlights:
builder.Configuration is available before builder.Build().
Values can come from appsettings.json, environment variables, user secrets, or even Azure Key Vault.
- This allows you to configure services, logging, or other web framework components dynamically.
Tim sets a breakpoint to verify that the LocalAPI URL is being retrieved correctly. He notes that this method works across all supported platforms, making it ideal for cross-platform development.
Handling Missing Configuration
Tim addresses scenarios where a configuration key might be missing or null. He shows that you can provide a default value using GetValue
var localApiBaseAddress = builder.Configuration.GetValue<string>("LocalAPI", "https://localhost:7654");This ensures that your applications are ready to run even if the configuration is not set, improving error handling and developer productivity. It also reduces the likelihood of runtime issues in cloud deployments or on different platforms.
Benefits of Early Configuration Access
Tim outlines several advantages of pulling configuration during setup:
Dynamic setup: Services like HttpClient can use environment-specific endpoints.
Avoid recompilation: Changing URLs or other settings no longer requires code changes.
Flexible logging and services: Configure loggers or other web framework services using configuration values.
- Supports multiple sources: Configuration can come from appsettings.json, environment variables, user secrets, or cloud key vaults.
He stresses that this approach keeps applications lightweight and maintainable while taking full advantage of the ASP.NET Core framework and the .NET platform.
Running and Testing the Application (6:03–7:36)
Tim demonstrates cleaning up the code and running the app. He shows that the LocalAPI URL is either pulled from configuration or falls back to the default value:
var localApiBaseAddress = builder.Configuration.GetValue<string>("LocalAPI", "https://localhost:7654");This flexible setup allows developers to continue building web apps without worrying about hardcoded URLs or repeated pull requests for minor changes. It also ensures that cross-platform deployments on Linux, Windows, or macOS are seamless.
Conclusion
Tim Corey concludes by emphasizing the simplicity and efficiency of accessing configuration during setup in ASP.NET Core / .NET 8. By using:
builder.Configuration.GetValue<T>()developers can:
Configure services dynamically
Avoid hardcoding values in the framework
Support multiple environments and platforms
- Improve developer productivity and error handling
This technique is essential for creating secure, maintainable, and cloud-ready web applications. Tim’s practical demonstration highlights how even a lightweight, cross-platform ASP.NET Core application can be robust, flexible, and ready for modern web development.
Tim Corey’s video is a must-watch for developers aiming to master configuration management in ASP.NET Core. By following his steps, you can ensure that your apps are flexible, maintainable, and ready for cross-platform deployment, cloud environments, and modern web architecture.

