Handling common JavaScript files in Visual Studio 2010

We're beginning work on a couple of fully JavaScript-dependent web apps (our previous apps have been ASP.NET MVC, with JavaScript 'goodness' sprinkled over-the-top).

We have a few files that will be shared across the board, and it would be nice to store these files in a Common project, and 'Add As Link' them into individual projects (as would be possible with compiled code).

Obviously this doesn't work with something like JavaScript as the file isn't actually 'there' in the correct location.

Does anyone have any suggestions on keeping a single version of a shared JavaScript file, for use across multiple projects?


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 at http://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

  • In the project that is going to 'import' the common code, I simply add the common .js file 'As Link' within /Assets/js/.
  • Go to that new addition's Properties and set 'Copy to Output Directory' to 'Copy if newer'.
  • 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 binAssetsjs - 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

    链接地址: http://www.djcxy.com/p/43690.html

    上一篇: visual studio 2010条件引用

    下一篇: 处理Visual Studio 2010中的常见JavaScript文件