I have three configuration files, one for each environment:
If I set ASPNETCORE_ENVIRONMENT to dev, I get a runtime exception complaining about not being able to find appsettings.dev.json. I tried adding
"copyToOutput": [
"appsettings.dev.json"
]
to the buildOptions
section in project.json but it doesn't seem to have any effect.
Is there another way I can force appsettings.dev.json to be copied to the output directory?
In my case, appsettings.json is not being copied for unit tests.
If you right click the file and choose Properties, this dialog will come up. Change Build Action to Embedded resource and the file will be copied to the bin folder for the unit test to pick up.
If you want to copy a file to your output folder you can add this to your csproj file:
<ItemGroup>
<None Update="appsettings.IntegrationTests.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
This works with dotnet build/test/run vscode & visual studio
Including this in "project.json" works for me:
...
"publishOptions": {
"include": [
"wwwroot",
...
"appsettings.json",
"appsettings.Development.json",
...
]
},
...
In solution explorer right click on your file that want to be copied to output (in your case appsettings.dev.json
) and hit Properties, In Properties window pane set Copy To Output Directory option to Copy Always. By doing so, every time you build your project, your file will be copied into output directory (your project bin or release directory depending on your build profile)
Have you set the base path where appsettings.dev.json is placed?
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With