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 ifrastructure to make this easy.
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:
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.
You should change build action to embedded resource for all files you want to use in a web application.
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:
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.