Embedded Resource Files

    Introduction

    In a web application, there are javascript, css, xml... files used by clients. They are generally added to a web project and published as seperated files. Sometimes, we need to package some of these files into an assembly (a class library project, a dll file) and distribute as embedded resource files in the assembly. ASP.NET Boilerplate provides an infrastructure to make this easy.

    Create embedded files

    We first should create a resource file and mark it as embedded resource. Any assembly can contain embedded resource files. Assume that we have an assembly named 'Abp.Zero.Web.UI.Metronic.dll' and it contains javascripts, css and image files:

    Embedded Javascript Files

    We want to make these files usable in a web application. First, we should make build action to embedded resource for files we want to expose. I select metronic.js file, go to properties window (F4 as shortcut) and change it.

    Mark file as embedded resource

    You should change build action to embedded resource for all files you want to use in a web application.

    Expose embedded files

    ASP.NET Boilerplate makes it easy to expose embedded resource files. It's a single line of code:

    WebResourceHelper.ExposeEmbeddedResources("AbpZero/Metronic", Assembly.GetExecutingAssembly(), "Abp.Zero.Web.UI.Metronic");

    This code generally stands in Initialize method of the module. Let's explain parameters:

    • First parameter defines root folder for files. It matches to root namespace.
    • Second parameter defines the Assembly contains files. In this example, I passed the assembly contains this code. But you can pass any assembly contains embedded resources.
    • And the last one defines root namespace of files in the assembly. This is the 'default namespace' plus 'folders in the assembly'. Default namespace is generally same as assembly's name but can be changed in properties of the assembly. Here, default namespace is Abp (I changed it), so Metronic folder's namespace is 'Abp.Zero.Web.UI.Metronic'.

    Consume embedded files

    Consuming embedded files is straightforward:

    <script type="text/javascript" src="~/AbpZero/Metronic/assets/global/scripts/metronic.js"></script>

    ASP.NET Boilerplate understands this is an embedded file and gets file from dll exposed before. While it simply works, you can use IncludeScript HtmlHelper extension method of ASP.NET Boilerplate in a razor view:

    @Html.IncludeScript("~/AbpZero/Metronic/assets/global/scripts/metronic.js")

    This will produce the same script tag as shown below:

    <script src="/AbpZero/Metronic/assets/global/scripts/metronic.js?v=635438748506909100" type="text/javascript"></script>

    Only difference is the v=635438748506909100 parameter. This prevents faulty caching of the script by browsers. This value changes only when your dll re-built (actually, it's the last write time of the file) and browser does not cache if it changes. So, using IncludeScript is the suggested way. It also works for non-embedded, physical files. There is also IncludeStyle method for css files.