Embedding JavaScript files into your DLL’s

 

This isn’t strictly related toSharePoint development, as you can do this with any ASP.NET page, but basicallythe theory here is that you can avoid the need to deploy files (such asJavaScripts, images, CSS sheets, whatever) to your sites by embedding themwithin your DLL’s. The reason I have been looking at this from a SharePointpoint of view is that if you have resources that you want to know will never betouched, you can go and put them in here instead of deploying them to your 12hive. The added bonus is that you can un-clutter the 12 hive a little by havingless of your custom files in there as well.

ASP.NET provides the special URL of“WebResource.axd” to provide these files. Essentially you call the page with anencoded reference to the file you want and the time it was last modified to getthe file back out. You can generate the required URL for each file throughcode, so don’t worry too much about getting that one right on your own :-)

So for this example I’ll use aJavaScript file as the example, add it to your Visual Studio project. Select itin the Solution Explorer window, and in the properties window you should seethe “Build Action” property – this tells the compiler what to do with the file,and for a JS file by default it will say none. Change this to “EmbeddedResource”.



 

An example of the build action property

Next, open the AssemblyInfo.cs file(usually appears under the ‘properties’ folder). In here we need to add areference to the file and tell the compiler what it’s MIME type is. This waywhen WebResource.axd is called it can build the response with the appropriateMIME type

[assembly:WebResource("DefaultNamespaceOfProject.FolderName.MyScript.js","text/javascript")]

The way the string here is made up toidentify your file is like this:

·Start with the default namespace of your project (set inthe properties window)

·Add the path of any folders or sub-folders to where thefile is within the project. So if you have your file in a folder called“scripts” which is inside a top level folder called “WebResources” you wouldhave “WebResources.Scripts”)

·Finish with the name of the file itself

Once the reference is in AssemblyInfo.csyou are now free to reference it in code where ever you need to. A commonexample I came across was needing to add JavaScript to the page to go with aspecific web part. To inject a reference to your file into a page through codethough you use something like this:

this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(),"DisableNameActiveX",

this.Page.ClientScript.GetWebResourceUrl(this.GetType(),"DefaultNamespaceOfProject.FolderName.MyScript.js"));

The above line is broken into two parts– firstly the Page.ClientScript.GetWebResourceUrl() method. This will get thecomplicated encoded URL that is required to get to the file we are asking for.This is the best way to get this URL out because of the fact that the URL itgives you includes a time stamp of when the file was last modified, so if yourDLL changes, so will the URL required to get to the resource. But once we havethe URL to our JS file, its just a simple call toPage.ClientScript.RegisterClientScriptInclude() to tell it to add the referenceto the JS file in the section of the document. In fact if you look through theHTML that SharePoint spits out you will see in a few places that it calledWebResource.axd for JavaScript and images in its own code.

There is one other thing worthmentioning here before you run off and start doing this – the potentialperformance hit. It’s not something I have looked in to a whole lot, but I havenot noticed any significant change in performance dealing files up this way,although logic does sorta tell me that serving the file up from the layoutsfolder on the file system is going to be quicker than dealing it up from insideof a DLL, so that is something to keep in mind when deciding to do things thisway or not.

 

Source: http://blog.brianfarnhill.com/2009/06/03/embedding-javascript-files-into-your-dlls/

posted @ 2012-04-04 14:28  iEden  阅读(187)  评论(0编辑  收藏  举报