Handling common JavaScript files in Visual Studio 2010 Handling common JavaScript files in Visual Studio 2010 javascript javascript

Handling common JavaScript files in Visual Studio 2010


I know this issue is ancient, but still wanted to put forward my solution because it is a bit simpler than beardtwizzle's.

You can ensure that Visual Studio copies all linked files to where you placed the link in Visual Studio's Solution Explorer by adding this at the end of your .csproj file:

  <Target Name="CopyLinkedContentFiles" BeforeTargets="Build">    <Copy SourceFiles="%(Content.Identity)"           DestinationFiles="%(Content.Link)"           SkipUnchangedFiles='true'           OverwriteReadOnlyFiles='true'           Condition="'%(Content.Link)' != ''" />  </Target>

I've described how this works in my blog post athttp://mattperdeck.com/post/Copying-linked-content-files-at-each-build-using-MSBuild.aspx


In the end, this is how I've achieved it. It may not be to everyone's taste - but worked a treat for me.

Note: In all of our projects, static resources are in a root directory called 'Assets', so for example JavaScript is always in /Assets/js/ and CSS /Assets/css/.

Solution

  1. In the project that is going to 'import' the common code, I simply add the common .js file 'As Link' within /Assets/js/.
  2. Go to that new addition's Properties and set 'Copy to Output Directory' to 'Copy if newer'.
  3. Now I simply edit the project's post-build event command line to the following:xcopy /Y /E "$(TargetDir)\Assets" "$(ProjectDir)\Assets"

When the project builds, it copies the imported files to \bin\Assets\js - the post-build event then takes a copy of those over to the project directory - in time for the site to use them.


The correct solution is embedding javascript/css files in your project. You can do this by using WebResources.axd. This is the official way microsoft embeds js in its controls. (Like validation controls)

You can find a good tutorial on: http://www.4guysfromrolla.com/articles/080906-1.aspx