Tag Archives: Embedded Resources

Output embedded resources into Razor views

In relation to my ASP.NET MVC Areas in separate projects thing I needed to share some tiny bits of javascript and css between pages in this separate project. Could probably be solved better by looking into the bundling stuff, but as a quick fix I decided to just compile the javascript and css files into the assembly and output them raw into my views. Quick and simple.

  1. Set the Build Action of your js and css file to Embedded Resource
  2. Add this extension method to a static class.
    public static IHtmlString RawResource(this HtmlHelper html, Type type, string resourceName)
    {
        using (var s = type.Assembly.GetManifestResourceStream(resourceName) ?? new MemoryStream())
        using (var r = new StreamReader(s))
        {
            s.Position = 0;
            return new HtmlString(r.ReadToEnd());
        }
    }
  3. Make sure the extension method is available in your view
    @using The.Namespace.Of.My.Static.Class
  4. Use the method in your view
    <script type="text/javascript">
        @Html.RawResource(typeof(AnyClassInAssembyOfEmbeddedResource), "Default.Namespace.Of.Project.Resources.file.js");
    </script>

    <style type="text/css">
        @Html.RawResource(typeof(AnyClassInAssembyOfEmbeddedResource), "Default.Namespace.Of.Project.Resources.file.min.css")
    </style>

Easy peasy. And slightly hackish and ugly… let me know if you have smoother, cleaner bundling approach 😉

If you’re having problems figuring out the right name of your embedded resource, see my post on how to use embedded resources for how to list them all out.

C#: How to use assembly embedded resources

After some digging around, I found that it actually wasn’t that difficult at all.

Getting it in

Putting something in an assembly as an embedded resource is pretty easy. At least if you are using Visual Studio. Just add the file to your project, click on it, and then under Properties set Build Action to Embedded Resource. And thats it!

Getting it out

Lets say we want an image called hello.png in a folder called Wopdidoo as a Stream.

If we are executing code in the same assembly, we can do as follows:

Assembly assembly = Assembly.GetExecutingAssembly();
Stream imageStream = assembly
    .GetManifestResourceStream("DefaultNameSpaceOfAssembly.Wopdidoo.hello.png");

If you are not executing code in the same assembly you just have to get that assembly reference in a different way. I often use Assembly.GetAssembly(typeof(T)), where T is some type you know exists in the same assembly as the file you want. The rest is the same.

As far as I know, you use the stream as any other stream. Not sure if it is writable though? Probably not… let me know if you have some brilliant clues on that matter 🙂

❓ Remember to Dispose it when you are done.

Finding it

I sometimes find it a bit difficult to figure out that string which identifies the resource. I then often use the following code to “find” it:

foreach (string s in assembly.GetManifestResourceNames())
    System.Diagnostics.Debug.WriteLine(s);

It basically just scans through all the resource names and prints them out to the debug console 🙂