Reputation: 1121
I have an ASP.NET core application with the following configurations:
public Startup(IHostingEnvironment hostingEnvironment)
{
_configuration = new ConfigurationBuilder()
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", true)
.AddEnvironmentVariables()
.Build();
}
When I publish the application and run it using the dotnet MyApp.dll
inside the application directory, it runs with no problems. When I execute the the command dotnet /dir1/dir2/MyApp.dll
, it fails to load the appsettings.json file. I did a little digging and found out that the ContentRootPath
is set to the directory I'm running the dotnet
command from and not the actual directory of the application. Can anyone tell me how to fix this issue?
Upvotes: 8
Views: 10410
Reputation: 6943
Similar to Adrian Massey. I had a project launching from VS with the wrong working directory, but only when using IIS.
I deleted all .vs files and reorganized and saved my sln file so that the projects were in alphabetical order.
I was looking for any bad references or other anomalies. After saving, a forced reload occurred.
Now my project no longer picks up the working directory of another project.
Upvotes: 2
Reputation: 39
I came across a scenario where if you have similar folder names it sets the ContentRootPath of the wrong project.
i.e.
foo.api/foo.api.csproj
or
bar.api/bar.api.csproj
would show the ContentRootPath for the project:
api/api.csproj
so it loaded an appsettings file, just not the right one.
To fix, I renamed the project folders without the '.' separators
Upvotes: 0
Reputation: 429
If you are having this issue when debugging within VS Code this can be caused by incorrect configuration in the .vscode\launch.json file
you need to make sure the "cwd" parameter for your build points at your project - so in this case it would need to be:
"cwd": "${workspaceFolder}/src/project"
Then the hostingEnvironment.ContentRootPath value will be correct when running Startup.
Upvotes: 2
Reputation: 71
I have the same problem. I often store my code in a /src folder which contains multiple projects. So from the CLI I would like to do the following:
cd /c/code/repo
dotnet restore /src/solution.sln
dotnet build /src/solution.sln
dotnet run /src/project/project.csproj
The first 2 work no problem but the CLI for .NET Core 2.0 does not recognize .SetBasePath(env.ContentRootPath) as the path where the project is being built to. Instead it uses the current path I am in on the CLI.
This results in:
Unhandled Exception: System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'C:\code\repo\appsettings.json'.
The only way I can get it to work is if I go the that directory:
cd /src/project
dotnet run project.csproj
Which is fine but if I want to rebuild the solution then I need to navigate back to /src where my .sln is located which is very annoying.
Seems like a bug in the CLI.
Upvotes: 4