Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ensure that appsettings.dev.json gets copied to the output folder?

I have three configuration files, one for each environment:

  1. appsettings.json -> production
  2. appsettings.dev.json -> development
  3. appsettings.stg.json -> staging

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?

like image 815
Alex G. Avatar asked Jul 04 '16 06:07

Alex G.


5 Answers

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.

enter image description here

like image 128
Jess Avatar answered Oct 19 '22 05:10

Jess


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

like image 40
Marius Avatar answered Oct 19 '22 07:10

Marius


Including this in "project.json" works for me:

...
"publishOptions": {
  "include": [
    "wwwroot",
     ...
    "appsettings.json",
    "appsettings.Development.json",
    ...
  ]
},
...
like image 38
Fabricio Koch Avatar answered Oct 19 '22 05:10

Fabricio Koch


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)

like image 32
Code_Worm Avatar answered Oct 19 '22 06:10

Code_Worm


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);
like image 7
Ivan Zaruba Avatar answered Oct 19 '22 07:10

Ivan Zaruba