Sergey
Sergey

Reputation: 41

ASP.NET MVC 6: How to add resource file using project.json?

I'm using an assembly that needs an external configuration file to be in bin folder to work properly. It seems that new way to do it in MVC 6 is to include reference in project.json file. Despite the detailed project.json docs I had no success in it.

Upvotes: 3

Views: 1303

Answers (2)

RonC
RonC

Reputation: 33791

When I try to use "resource" in the project.json and then compile my project the output window indicates the following:

warning DOTNET1015: The 'resource' option is deprecated. Use 'embed' in 'buildOptions' instead.

So I guess that's the preferred approach now.

Upvotes: 1

T. Yates
T. Yates

Reputation: 192

The project.json file should contain a "resource" definition with your config file. You can then access the resource using a manifest resource.

project.json

{
  ...
  "resource": "bin/somefile.config",
  ...
}

This can also include wildcards and arrays of values if necessary.

*.cs

string resourceName = "ProjectName.bin.somefile.config";
Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

Note that the config file included slashes without a project name, but the resulting resource name includes the project name and converts slashes to dots.

Troubleshooting

If you are getting back null for your resource stream, it can help to set a breakpoint and examine the output of Assembly.GetExecutingAssembly().GetManifestResourceNames().

Upvotes: 2

Related Questions