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

好久不见. 提交于 2019-11-27 14:03:59

问题


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?


回答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.




回答2:


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);



回答3:


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

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



回答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)




回答5:


Assumes you are using Visual Studio:

Typically for services in the past I have had other devs set up 'Build Events' that ensure that certain things in the project get built. Typically it would be in ASP.NET project properties for Visual Studio 2015 as an example: >'Build Events'(left pane)>Click 'Edit Post-build'. Write in command line instructions of how the project should build to output. EG:

mkdir "$(ProjectDir)bin\Setup"  

create a directory under bin called 'Setup'

Del "$(ProjectDir)bin\Setup\*.* /Q"

Deletes any existing items in the newly created bin

copy "$(TargetDir)*.dll"  "$(ProjectDir)bin\Setup"

Copies dll's from build location to the new setup location

Now if you are doing a build process where your 'Configuration Manager' is doing a different output for each environment and you just want each of them at all times you would probably need to traverse up a level, find those bins by their name of 'dev' or 'stg' and then copy them back. You may be able to create or list a variable that VS knows for the environment that matches to your JSON data as well.



来源:https://stackoverflow.com/questions/38178340/how-can-i-ensure-that-appsettings-dev-json-gets-copied-to-the-output-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!