C Realization Method of Compiling javascript File into dll File

  • 2021-08-21 21:14:43
  • OfStack

This article illustrates the method of C # to compile javascript file into dll file. Share it for your reference, as follows:

Step 1: New project Add js file Write a test Function (JScript. js) Click JS file properties. Change the build operation to an embedded resource

Step 2: Add a class file to the project file that inherits the System. Web. UI. WebControl class and overrides the control's pre-rendered methods


public class ClientScript:System.Web.UI.WebControls.WebControl
{
   protected override void OnPreRender(EventArgs e)
   {
     if (this.Page != null) {
       ClientScriptManager manager = this.Page.ClientScript;
       // Register resource scripts 
       manager.RegisterClientScriptResource(typeof(ClientScript),"JsControl.JScript.js");
     }
     base.OnPreRender(e);
   }
}

Step 3: Expand the properties directory and lay down the AssemblyInfo. cs file. Add the assembly information as the embedded resource type

[assembly:System.Web.UI.WebResource("JSControl.JScript.js","appliaction/x-javascript")]

Step 4: Rebuild the class library and compile it into an dll file to add to the project reference:


<%@Register Assembly="JsControl" Namespace="JsControl" TagPrefix="zhi" %>
<zhi:ClientScript runat="server" ID="rs1" />

OK complete!

I hope this article is helpful to everyone's C # programming.


Related articles: