18

In my MVC application I use external config files to keep clean web.config. Some files are common and I added them to project as link from one location. For those files I set Copy option to Copy always and those files are copied to destination folder and I see them. But when I try to open home page in the browser I see "Unable to open configSource file" error. When I remove links to files and just add them (no link) everything works good. Any idea what may cause this error?

Alexandr Zaitsev
  • 421
  • 1
  • 3
  • 9

6 Answers6

24

Finally I found what was the issue. When use Copy always, files are copied to bin folder. But files are searched in the virtual directory not in the bin. So I added post build task which copies files to correct destination.

Alexandr Zaitsev
  • 421
  • 1
  • 3
  • 9
  • 3
    It's worth noting that in the case of connectionStrings.config, it must be in the same folder as web.config to work correctly: https://www.asp.net/identity/overview/features-api/best-practices-for-deploying-passwords-and-other-sensitive-data-to-aspnet-and-azure – Ramón Jan 17 '17 at 16:57
9

After trying the xcopy solution, which failed, the follow worked for me:

Right click the cstrings.config, go to Properties.

Set the property Copy to Output Directory value to Copy always

Edgar Froes
  • 768
  • 8
  • 20
  • I had a issue with log4net config file and this helped me, thanks! Steps: right click log4net.config and select [Properties], lastly set [Copy to Output] file property => Always Copy – Tumelo Apr 15 '20 at 08:39
8

I added the post build event as suggested. But the exact steps are:

  1. Right click on the project and navigate to properties
  2. Click on the Build Events tab
  3. In the Post-build event command line: box I added:

xcopy /s "$(ProjectDir)\bin\Config" "$(ProjectDir)\Config"

John Lay
  • 301
  • 4
  • 12
6

I suffered from the same problem. In my case, config file's 'Build Action' property was the cause. ( Right click on the config file > Properties > Build Action )

The 'Build Action' value was set to 'None'. After I changed it to 'Content', the problem sovled. ( I am not sure if it was 'a Visual Studio's bug' or 'my click-by-mistake' that made it set to 'None'.

김성훈
  • 99
  • 1
  • 6
5

See https://stackoverflow.com/a/38408867/2240453 Long story short: You can add

<connectionStrings configSource="bin\Connections.config">  
</connectionStrings>

to differentiate configuration (deploy/release so debug/deploy version) You can add transformation . It's quite handy to use them anyway.

(Yes, i know the topic is solved, but maybe someone will find this information useful as I did)

Tariktiri
  • 149
  • 1
  • 10
2

Right click on the config file > Properties. Set the setting like below screenshot

enter image description here

Jaydeep Shil
  • 1,894
  • 22
  • 21