Earn More by Sharing What You Love
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Tim Corey
8m 02s
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.
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 config
However, 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.
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.
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.
Tim demonstrates how to pull configuration values before dependency injection:
var localApiBaseAddress = builder.Configuration.GetValue<string>("LocalAPI");
Key points Tim highlights:
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.
Tim addresses scenarios where a configuration key might be missing or null. He shows that you can provide a default value using GetValue<T>:
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.
Tim outlines several advantages of pulling configuration during setup:
He stresses that this approach keeps applications lightweight and maintainable while taking full advantage of the ASP.NET Core framework and the .NET platform.
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.
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:
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.
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!
Join our newsletter, you’ll get exclusive access on article updates. We value your privacy