Reputation:
What is the difference between .SetBasePath and .UseContentRoot in Startup configuration?
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
How can I use IConfiguration from my integration tests?
Upvotes: 5
Views: 3088
Reputation: 4889
For dotnet6/7 in 2022 with top-level statements, UseContentRoot
is not available, just set the current directory before WebApplication.CreateBuilder
works for me.
var dllFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Directory.SetCurrentDirectory(dllFolder);
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.SetBasePath(dllFolder);
Upvotes: 2
Reputation: 93293
SetBasePath
is an extension method for IConfigurationBuilder
, which sets the path to use when locating configuration files:
Sets the FileProvider for file-based providers to a PhysicalFileProvider with the base path.
For example, when locating the appsettings.json
file you've specified in your question, it will look in the path retrieved using Directory.GetCurrentDirectory()
UseContentRoot
is an extension method for IWebHostBuilder
, which sets the contentRoot
key for the Web Host:
This setting determines where ASP.NET Core begins searching for content files, such as MVC views.
The default value used for the contentRoot
key is:
[...] the folder where the app assembly resides.
This means that, for a typical setup, the path for both will end up being the same, but this is not a requirement.
Upvotes: 6