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.
- Set the Build Action of your js and css file to Embedded Resource
- Add this extension method to a static class.
- Make sure the extension method is available in your view
@using The.Namespace.Of.My.Static.Class
- 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.