Understanding ASP.NET Core hosting configuration parameters

Posted: (EET/GMT+2)

 

ASP.NET Core gives you many flexibile options in how the hosting layer is configured. If you've used WebHostBuilder or Host.CreateDefaultBuilder() in your applications without thinking too much about it, it might be worth looking at what parameters you can control and where these values come from.

When the application starts, ASP.NET Core pulls configuration from multiple sources in the following order:

  • JSON files: appsettings.json and appsettings.<Environment>.json
  • Environment variables: these are often important in container deployments
  • Command-line arguments to your EXE or "dotnet"
  • User secrets: (during development)

The hosting layer itself also exposes configuration options. A few common ones:

// Example of configuring the Kestrel host
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.ConfigureKestrel(options =>
            {
                options.Limits.MaxRequestBodySize = 10 * 1024 * 1024; // 10 MB
                options.AddServerHeader = false;
            });

            webBuilder.UseStartup<Startup>();
        });

You can also override the default hosting settings by providing values through the same configuration system. For example, to set the URLs your app listens on:

set ASPNETCORE_URLS=http://localhost:5005

This is especially useful when you are deploying to Azure App Service, containers, or Windows services. Instead of rebuilding your app, you adjust behavior through configuration alone.

If you haven't explored the hosting configuration mechanism yet, it's worth doing. You might find that your application needs fewer custom extension methods than you think.