Skip to content

ASP.NET Core (.csproj) Embedded Resources

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.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


ASP.NET Core MVC Embedded ResourcesI’ve previously blogged about how to have embedded resources in ASP.NET Core, however that was with the older project.json.  I figured I would show how the project.json translates to the new csproj format.  If you are using Visual Studio 2017, the process via UI should feel pretty standard. If you have any questions with this post, please let me know in the comments or on Twitter.  

Visual Studio 2017

For my example, I’ve included a json file called fake.json I pulled from Mockaroo.  Here’s a screenshot of VS2017 that shows the Data/fakedata.json file in my solution. Solution If you right click on the fakedata.json, and go to Properties, you will be presented with a familiar properties dialog.  This is where you can set the Build Action to Embedded Resource File Properties Dialog

.csproj

If you prefer to edit the .csproj file by hand or are using another IDE/editor, you simply have to add two new ItemGroup‘s.
Make sure the path to your files are relative to your csproj.  Here’s a full copy of my solution.

GetManifestResourceStream

Now in your code, you can access the Assembly API to get our your embedded resource.
Note that GetManifestResourceStream takes a string that represents your file.  The path would be the assembly name and the folder structure is used as a namespace. Also  in the above code, the entry assembly (EntryAssembly.csproj/dll) is being used.  If you’re embedded resource is in another assembly, you will need to use different reflection code to get that assembly.

Questions

The source for this demo is available on GitHub. If you have any questions regarding embedded resources or converting from project.json, I’d love to hear from you.  Please leave a comment or on twitter.
 

4 thoughts on “ASP.NET Core (.csproj) Embedded Resources”

  1. I hope you don’t mind the question here, but it seems related.
    With an ASP.NET Core 1.1 MVC site, how do I include in publishing a third-party DLL that is expected to live in the “bin” directory? It is a third-party ASP.NET ASPX application that a JavaScript plugin is attempting to call.
    Thank you.

      1. Sorry, didn’t see the original comment. Is the assembly coming from a nuget package? If the package is referenced, it will be copied to be bin directory assuming your code actually references it in code using a type from the referenced assembly. Top of head, having a post build action to copy the assembly to the bin folder?

Leave a Reply

Your email address will not be published. Required fields are marked *