Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
In my previous blog
about adding gzip compression when using NancyFX, I wanted to test the compression by outputting a large json data file in one of my endpoints.
At first I was just going to generate some random data and serialize it at runtime but then I found Mockaroo
, which provided a small dataset of random data that I could download.
So now that I had this generated file, I wanted to include the file in my project as an embedded resource in which would be read and at returned as output.
If you have any questions, please follow me on Twitter.
The project.json file is new to ASP.NET Core, which defines project information and dependencies, similar to the combination of a web.config, csproj files, package.config is in ASP.NET 4.
There is a resource section in the project.json which is for adding resources to your assemblies as embedded resources. The values are relative to the project.json file itself and allows you to specify glob patterns (wildcards).
In my demo project, I had a fakedata.json file which resides in the same path as the project.json file. Here is my project.json file, including the new resource section.
All of your resources will be included in the assembly when built. You can read those resources by getting a stream of the resource.
If you use a glob in your resource path to possibly include all files within a directory, but want to exclude specific files, you can do so with the resourceExclude.
If you are looking for more information about the project.json, I’d recommend taking a look at the GitHub wiki
. The asp.net docs are helpful but I couldn’t find a ton of info the project.json file specifically (yet). I assume the asp.net docs will improve over time and they are a work in progress.