I had an idea for quite a while now that involved the process of creating a NuGet package with (html) template files included in it. I wasn’t quite sure how to get my HTML files included in a NuGet package…so my search began…
Step 1: Set your files as an embedded resource
Set your build action of the files that you want included on“Embedded resource”
Step 2: Accessing the files from within the library
var assembly = typeof(MyLib.MyClass).GetTypeInfo().Assembly;Stream resource = assembly.GetManifestResourceStream("MyLib.SomeFolder.MyTemplate.cshtml");StreamReader reader = new StreamReader(resource);
string text = reader.ReadToEnd(); //hello world!
Notice how the path to the file is separated by dots. The dots are representing the file structure. MyLib/SomeFolder/MyTemplate.cshtml is how can you look at it.
So…what did we achieve?
We’re now able to deploy our library in a NuGet package and use it in any other project with the files embedded in our dll! In my case this is handy as I have some predefined HTML templates in my library. These will be used across multiple projects for some PDF generation. That does the job for me!
Enjoy!