ASPNET Kernel: Incorrect value for ContentRootPath

I have an ASP.NET kernel 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 dotnet MyApp.dllinside the application directory, it starts without any problems. When I execute the command dotnet /dir1/dir2/MyApp.dll, it does not load the appsettings.json file. I did a little work and found out that it is ContentRootPathinstalled in the directory in which I run the command dotnet, and not the actual application directory. Can someone tell me how to fix this problem?

+5
source share
3 answers

. /src, . CLI :

cd /c/code/repo
dotnet restore /src/solution.sln
dotnet build /src/solution.sln
dotnet run /src/project/project.csproj

2 , CLI .NET Core 2.0 .SetBasePath(env.ContentRootPath) , . , CLI.

:

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'.

, , - , :

cd /src/project
dotnet run project.csproj

, , /src, .sln, .

CLI.

+4

VS-, .vscode\launch.json

, "cwd" - :

"cwd": "${workspaceFolder}/src/project"

hostEnvironment.ContentRootPath Startup.

0

I came across a scenario in which, if there are similar folder names, it sets the ContentRootPath of the wrong project.

that is, foo.api/foo.api.csproj or it bar.api/bar.api.csproj will show ContentRootPath for the project: api/api.csprojso it uploaded the appsettings file, but not the right one.

To fix, I renamed the project folders without '.' separators

0
source

Source: https://habr.com/ru/post/1668247/


All Articles