Preface
For the sake of security and transplantation, I have read some documents about WebResource in the last two days. Here, I despise forums and blogs that are plagiarized. They are almost identical in a single search, and I have not said much !!
Thanks
1. MSDN does not really make what I want until this example appears, but it also brings some incomprehension.
2. Using WebResource. axd to access the built-in resources of accessories through a URL
3. bundling JS files in custom Server Control. This is a good article. Although it is not used, it is recommended
Question
First, let's take a look at my final directory structure (this is the project structure that uses WebResource. axd to access the built-in resources of the accessories through a URL (translated) the code downloaded from the original English site ):
Here, we need to describe the following points:
1. The following resource registration code is put in AssemblyInfo. cs, and the effect is the same before namespace of any class in DLL. (I personally suggest you put it in AssemblyInfo. cs for centralized management)
[Assembly: WebResource ("FunkyTextBox.Resources.test.jpg", "image/jpeg")]
2. The location of the resource file in the DLL is related to the access !! I put test.jpg in the graph in the root directory and in the Resources directory to access it differently. When registering Resources, it is based on this (that is to say, if it is in the root directory, the name of the registered resource is "FunkyTextBox.test.jpg ").
Now let's first analyze the original code architecture of FunkyTextBox, which is also the architecture of many online examples:
1. Copy the resource file to the project.
2. Write your own user control and inherit from WebControl, such as TextBox, that is, call the resource file within the DLL.
3. Register Resources in the user control (or in AssemblyInfo. cs)
Basically, we can see that the resource file is called inside the DLL and then the custom control is referenced from outside. Here, I mainly discuss the idea of directly referencing DLL internal resource files externally. I believe many of my friends are the same as me, copy the code of the DLL internal reference resource file and copy it to ASPX. How can all the images be copied? This includes registering httphandles to intercept WebResource. the axd does not work either. It wasn't until I saw that piece of code on MSDN:
Using System;
Using System. Web;
Using System. Web. UI;
Using System. Security. Permissions;
[Assembly: WebResource ("Samples. AspNet. CS. Controls. script_include.js", "application/x-javascript")]
Namespace Samples. AspNet. CS. Controls
{
[AspNetHostingPermission (SecurityAction. Demand, Level = AspNetHostingPermissionLevel. Minimal)]
Public class ClientScriptResourceLabel
{
// Class code goes here.
}
}
<% @ Page Language = "C #" %>
<% @ Import Namespace = "Samples. AspNet. CS. Controls" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">
Public void Page_Load (Object sender, EventArgs e)
{
// Define the resource name and type.
String rsname = "Samples. AspNet. CS. Controls. script_include.js ";
Type rstype = typeof (ClientScriptResourceLabel );
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page. ClientScript;
// Write out the web resource url.
ResourcePath. InnerHtml = cs. GetWebResourceUrl (rstype, rsname );
// Register the client resource with the page.
Cs. RegisterClientScriptResource (rstype, rsname );
}
</Script>
<Html>
<Head>
<Title> ClientScriptManager Example </title>
</Head>
<Body>
<Form id = "Form1" runat = "server">
The web resource path is
<Span id = "ResourcePath" runat = "server"/>.
<Br/>
<Input type = "text" id = "Message"/>
<Input type = "button" onclick = "DoClick ()" value = "ClientClick"/>
</Form>
</Body>
</Html>
To make it easy to see the effect, I changed the code for reading JS from DLL to the Code for reading images from DLL. The changes are as follows:
1. Change the ClientScriptResourceLabel namespace to FunkyTextBox.
2. Change the resource registration code to the following (note the resource path ):
[Assembly: WebResource ("FunkyTextBox.Resources.test.jpg", "image/jpeg")]
3. Add an image button for the ASPX page and change the corresponding reading result to the following:
<Script runat = "server">
Public void Page_Load (Object sender, EventArgs e)
{
// Define the resource name and type.
String rsname = "FunkyTextBox.Resources.test.jpg ";
Type rstype = typeof (ClientScriptResourceLabel );
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page. ClientScript;
// Write out the web resource url.
Imgpath. Src = cs. GetWebResourceUrl (rstype, rsname );
// ResourcePath. InnerHtml =
// Register the client resource with the page.
// Cs. RegisterClientScriptResource (rstype, rsname );
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> WebResources </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
The preceding access code can be simplified as follows: mgpath. Src = ClientScript. GetWebResourceUrl (typeof (FunkyTextBox), "FunkyTextBox.Resources.test.jpg ");
From the code above, we can see that the ClientScriptResourceLabel class is empty, and the only useful thing is to register the resource. Next we will comment out the resources in ClientScriptResourceLabel and register the resources to AssemblyInfo. cs. This makes me wonder! Type points to an empty class that can also display resources, but I cannot use this. GetType () or typeof (_ Default !! I guess the first parameter of GetWebResourceUrl only needs to be directed to the correct DLL space, and then the resource can be found !? Also, I can't specify the Type using Assembly. Load ("FunkyTextBox"). GetType (). I still feel a little learning :)
Now I can basically meet my requirements for direct access to internal resource files, but I only need an extra empty class to specify the type, which meets my requirements for the moment. Now I can consider putting JS in it, in this way, if you copy and generate a js src link for direct access, it will not work! This is where you can talk about it!
Supplement-- Download code []:
At that time, the Demo was written on the basis of others' source code: http://files.cnblogs.com/over140/FunkyTextBox.rar