<ABP document> embedded resource files:
Document directory
Content of this section:
- Introduction
- Create an embedded file
- Expose embedded files
- Use embedded files
Introduction
In a web application, the client contains javascript, css, xml, and other files. After being added to a web project, the file is published as an independent file. Sometimes, we need to package some of these files into an assembly (A Class Library Project, a Dll file) and deploy them in this Assembly as an embedded resource file, which provides an infrastructure, this is easy to handle.
Create an embedded file
First, we need to create a resource file and mark it as an embedded resource. Any assembly can contain an embedded resource file. Suppose we have a file named "ABC. zero. web. UI. metronic. dll assembly, which contains javascript, css, and image files:
We want to use these files in a web application. First, we should change the Build Action of the file to Embedded Resource ), select metronic. js file, go to the attribute window (the shortcut key is F4), and modify it.
In a Web application, you should change all the file generation operations you want to use to embedded resources.
Expose embedded files
You can easily expose embedded resource files with only one line of code:
WebResourceHelper.ExposeEmbeddedResources("AbpZero/Metronic", Assembly.GetExecutingAssembly(), "Abp.Zero.Web.UI.Metronic");
This code is usually put in the Initial method of the module. Let's explain its parameters:
- The first parameter defines the root directory of the file, which matches the root namespace.
- The second parameter defines the files contained in the Assembly. In this example, I pass the Assembly containing this code, but you can pass any assembly containing embedded resources.
- The last parameter defines the root namespace of these files in this set. It is the "default namespace" + "directory in this set ", the default namespace is usually the same as the name of the Assembly, but can be modified in the Assembly attributes. Here, the default namespace is Abp (I modified it ), therefore, the namespace of the Metronic directory is "ABC. zero. web. UI. metronic ".
Use embedded files
You can directly use the embedded file:
<script type="text/javascript" src="~/AbpZero/Metronic/assets/global/scripts/metronic.js"></script>
You can understand that this is an embedded file and obtain all the files exposed by the previous Dll. Although it can work simply, you can use the HtmlHelper Extension Method of "Export descript" in a Razor View:
@Html.IncludeScript("~/AbpZero/Metronic/assets/global/scripts/metronic.js")
This generates the following script Tag:
<script src="/AbpZero/Metronic/assets/global/scripts/metronic.js?v=635438748506909100" type="text/javascript"></script>
The only difference is the v = 635438748506909100 parameter, which will prevent the default cache of the browser script. This value is only used when your Dll is regenerated (essentially, the last modification time of the file) changes occur. If the changes occur, the browser will no longer cache the Old Ones. Therefore, we recommend that you use the IncludeScript method. It can also be used for non-embedded physical files, there is also a IncludeStyle Method for css files.