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

22,801

Solution 1

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

Solution 2

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

Solution 3

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

...
"publishOptions": {
  "include": [
    "wwwroot",
     ...
    "appsettings.json",
    "appsettings.Development.json",
    ...
  ]
},
...

Solution 4

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)

Solution 5

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);
Share:
22,801

Related videos on Youtube

Alex G.
Author by

Alex G.

Updated on June 11, 2021

Comments

  • Alex G.
    Alex G. almost 3 years

    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?

    • Maarten Bodewes
      Maarten Bodewes almost 8 years
      You've got an answer from Ivan. Maybe it's a good idea to indicate if that works for you?
    • J. Lennon
      J. Lennon over 7 years
      I couldn't find a solution to deploy a specific file per environment. You could configure to deploy all the project*.json files -stackoverflow.com/questions/37858312/… But that's not good option, the perfect scenario is the 1 to 1 file per environment (configuration name) - the bounty is open :)
    • Ben
      Ben over 5 years
      See this answer for a possible solution of environment specific appsettings.
  • Philip Pittle
    Philip Pittle over 7 years
    This will prevent the exception from being thrown, but doesn't answer the question of how to make sure the .dev.json file is copied.
  • VorobeY1326
    VorobeY1326 over 7 years
    Didn't work for me. It worked for one time cause project.json was changed and it caused project to rebuild. Only solution working — call rebuild instead of build. It works with both compile options and publishOptions.
  • Vladislav
    Vladislav about 7 years
    .. And check your .vscode/launch.json -> "cwd" path. Setting this from "${workspaceRoot}" to "${workspaceRoot}\src\myproject" worked for me.
  • Jacques
    Jacques over 3 years
    +1 - Originally had the same as this except the element name <None /> was <Content/>. Everything else was the same - but it didn't work. Changing it to None got it working
  • ryanwebjackson
    ryanwebjackson about 2 years
    @Jacques I encountered the same. I wonder why it works this way.
  • ryanwebjackson
    ryanwebjackson about 2 years
    I doubt "Embedded resource" is the only build action that will work (reading additional answers). Additional detail would be useful.