"Go" webresource implement embedded JS file in custom control

Source: Internet
Author: User
Tags file url

Resources in the class library need to be embedded in other projects.

References:webresource implementing embedded JS files in custom controls

1. WebResource Introduction

ASP. NET (1.0/1.1) provides us with a programming model for developing WebControl, so we get rid of the reuse of the include mode in ASP. However, the Web control development model provided by 1.0/1.1 is handy for working with components that do not have external resources such as image, CSS, and script, although many of the time it is external resources, we used to use script Page.register when developing controls ... Script () to embed the module, because the compact thing is more convenient for us to reuse, with a DLL can solve the problem.

The Web resources management model provided by ASP. NET 2.0 solves the problem of external resource management such as image, CSS and script. Webresourceattribute is a Metedata attribute that allows us to access resources embedded in the assembly.

2. Use of WebResource

When using an embedded server side resource, you must first embed the resource file embedded into our assembly assembly, and then add a reference to those files in the AssemblyInfo.cs file in application. Specific process:

① file Inline: Change the file "Build Action" attribute you want to embed into a resource embedded in

Once the ② file is embedded, you will need to refer to the embedded file below.

There are two methods of referencing:

Method One: Locate the AssemblyInfo.cs file and add the code:

[Assembly:webresource ("EWebapp.Upload.upLoad.js", "Text/javascript")]

At the same time, you need to add namespaces using System.Web.UI;

Method Two: In any class file, you can add a reference to the Declaration;

[Assembly:webresource ("EWebapp.Upload.upLoad.js", "Text/javascript")]

(On the top of the namespace.)

parameter Description: The first is the name of the resource, the second is the Mime-type name of the resource.
The name of a resource typically consists of three sections:< the default namespace .>< extension namespace .>< file name >
The default namespace is the namespace of the project, and the name of the resource file.
The intermediate extension namespace is the directory structure in the project. If your resource file is in the root of the project, the extension namespace is not needed, so write it directly. < The default namespace .> < filename;. However, if you generate directory results in your project, you will need to use an extended namespace. For example, suppose the above resource file exists in Directory JS:

[Assembly:System.Web.UI.WebResource ("Ewebapp.upload/js/upload.js", "Text/javascript")]

③ after the completion of the preparation work, the following can get the embedded resource file.

protected override void AddAttributesToRender (System.Web.UI.HtmlTextWriter writer)

{

Writer. AddAttribute ("onchange", "newupload (this);");

Base. AddAttributesToRender (writer);

}

Note: This section is primarily to append the onchange event to the control, or it can be added directly when the interface is created, such as:

F.attributes.add ("onchange", "javascript:newupload (this);");

public class Upload:System.Web.UI.WebControls.WebControl

{

protected override void OnPreRender (EventArgs e)

{

Page.ClientScript.RegisterClientScriptInclude ("UpLoad", Page.ClientScript.GetWebResourceUrl (this. GetType (), "eWebapp.Upload.upLoad.js"));

Base. OnPreRender (e);

}

}

④ generated pages such as:

<script src= "/webresource.axd?d=sfslyqlv-ru-lsltt0gpronfy7jkks9lj16lbul83y0oicwzcuhlxicfpygf1ysg0&amp;t= 633813613197656250 "type=" Text/javascript "></script>

One of the <script src= "/webresource.axd?d= ... is a call to a script resource.

Remark:

1. The file name of the embedded resource is similar to EWebapp.Upload.upLoad.js

Syntax:ewebapp.upload/upload.js-->ewebapp.upload.upload.js

The resource name must be the same as declared in the AssemblyInfo.cs file. If the resource name is wrong or the file does not exist, the footstep generated in the address will not reach the expected result.

2. The resource file must be embedded within the assembly.

3. Return the resource file URL analysis:

Webresource.axd?d= the encrypted identifier & T = timestamp value.
which

"D" represents a request for a WEB resource. (encrypted identifier)
"T" is a timestamp to the assembly, which helps in determining if a change has been made to a resource request.

Tips:

1, see online Some people say this does not work, because of the default namespace vs a Ghost.

Problem Description: VS automatically adds the default namespace to the front of the resource file.

Workaround: simply change the default namespace to empty.

However, in the course of the study, I did not encounter this problem. Perhaps if you do not know what is wrong, you can use the Anti-compilation tool reflector to see if the generated resource file is correct.

"Go" webresource implement embedded JS file in custom control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.