0

I have a small WebApi project, and from inside this project I want to load a resource JSON file with some information. Inside my project I created a folder called Resources, and inside this Resources folder I added a data.json file.

I want to know how can I use this data.json file inside of my project. I did some reading and wasn't able to find a BuildAction that puts this resource file on the same directory as my binaries so I can use it.

I would like to do something like this:

string json = File.ReadAllText(@"..\Resources\data.json");

However if I do this I get this exception:

"Could not find a part of the path 'C:\Program Files (x86)\Resources\data.json'."

I understand very little about ASP.NET, but is there a way I can accomplish this?

Community
  • 1
  • 1
adamasan
  • 1,092
  • 1
  • 12
  • 33

1 Answers1

0

It's not the build action you would set, it's the 'copy to output directory' you want to set to either 'always' or 'when newer'. That will copy it to a resources folder under your bin folder. Then, to get the data out, you would use Url.Content("~/bin/resources/data.json") assuming you were in a controller. That should resolve properly.

If this data doesn't change, the other option would be to make it an Embedded Resource which would then incorporate it into the web site's dll.

MikeS
  • 1,734
  • 1
  • 9
  • 13