Tag Archives: visual studio

Javascript as C# code

One of the most handy things I created during my daily job as a .NET developer is a system to automagically include my javascript code libraries into every new project I’m going to develop. It’s a sort of javascript dll…no wait IT ACTUALLY IS a .dll file! =)
Here’s the trick…

Preparing the recipe

  • Create a new library (.dll) project … name it “My.Common.Js” (it’s just an example)
  • Include your javascript file (i.e. “myJsFile.js“) into the project
  • Include a folder that contains the minified version of the script (something like “minified/myJsFile.min.js“) into the project
  • Right click on the “myJsFile.min.js” and choose: Build Action “Embedded Resource”

Mix them all

Into the .cs file include the following (at the top, under the “using” block):

[assembly: WebResource("My.Common.Js.minified.myJsFile.min.js", "application/x-javascript")]

Let’s define a class that uses these files:

namespace My.Common
    public class JsUtil

The JsUtil class has one interesting private function that takes the name of a resource and create its phisical file into the given path, including its inclusion (sorry for the joke) into the given page:

private static void dumpResource(Page p, String rezname, String path, String inclPath, Boolean toTheEnd)
  String jsns = "My.Common.Js.minified.";
  // fix path
  if (!path.EndsWith(@"\")) path += @"\";
  if (!inclPath.EndsWith(@"/")) inclPath += @"/";
  String fout = path + rezname;
  String version = "?ver=1";

  // recreate the file only if needed
  if (!System.IO.File.Exists(fout))
    // read the embedded resource
    String rez = String.Empty;
    System.Reflection.Assembly a = System.Reflection.Assembly.GetAssembly(typeof(JsUtils));
    Version v = a.GetName().Version;
    version = "?ver=" + v.Build.ToString();
    using (StreamReader sr = new StreamReader(a.GetManifestResourceStream(jsns + rezname)))
      rez = sr.ReadToEnd();

    // write it into a file
    if (!Directory.Exists(path)) Directory.CreateDirectory(path);
      using (StreamWriter sw = new StreamWriter(fout))
      // we're racing...let them win

  // include it into the page 
  System.Web.UI.HtmlControls.HtmlGenericControl headctrl = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
  headctrl.Attributes.Add("type", "text/javascript");
  headctrl.Attributes.Add("src", inclPath + rezname + version);
  if (toTheEnd || p.Header.Controls.Count == 0)
    p.Header.Controls.AddAt(0, headctrl);

Ok, now it’s easy. My JsUtil class exposes a very minimal static method:

public static void Include_myJsFile(System.Web.UI.Page page, String jsPath, String inclPath)
  dumpResource(page, "myJsFile.min.js", jsPath, inclPath, true);

Serve hot

Somewhere in your page:

// filesystem path
String filepath = HttpContext.Current.Server.MapPath("~") + @"\JS\my.common";
// js path
String includepath = @"../JS/my.common"; // js format
// check
if (!Directory.Exists(filepath)) Directory.CreateDirectory(filepath);
// include
JsUtils.Include_myJsFile(this, filepath, includepath);
Leave a comment

Posted by on 2013/12/27 in dev


Tags: , , , ,