2

normally I would go

<script src="~/Scripts/Controls/SomeScript.js"></script>

and this finds SomeScript.js in the "Scripts/Controls" folder of the same project that the ASP.NET .cshtml page is in.

The time has come however to share the javascript file between two projects. I need to move it to a library project.

What would the value of the src attribute have to be to locate the javascript file in a completely different project in the same solution? Do I have to do something else? How is this problem usually solved?

I have tried "Copy to output directory" on the "SomeScript.js" properties page and this copies the script and its containing folder to the 'bin' directory. However chrome reports "Not allowed to load local resource" when trying to access .js files in the bin folder.

Isaac Bolinger
  • 7,328
  • 11
  • 52
  • 90
  • http://stackoverflow.com/questions/6107464/handling-common-javascript-files-in-visual-studio-2010/6145674#6145674 Quite a lively discussion of the same thing over there. – Isaac Bolinger Nov 26 '14 at 01:20

2 Answers2

1

The folder which your scripts exist must be within the project tree, especially if you are planning on publishing the app to a remote server. Visual Studio provides an option to Add Existing Items as a link, which would allow you to symlink items stored outside your project tree into a folder within your project tree. Any changes to the original file will be reflected in your project; however if the file is deleted, the link will remain, broken.

You must also add the following to the end of the project .csproj file before the Project close tag.

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

This edit to the .csproj file will cause visual studio to actually copy the linked file to the location of the link at the end of the build.

In this way, you can still reference ~/Scripts/Controls/SomeScript.js in your project.

Isaac Bolinger
  • 7,328
  • 11
  • 52
  • 90
Claies
  • 22,124
  • 4
  • 53
  • 77
1

use this reference to resolve your issue: Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?

seems that the best way would be to use / at the beginning - that is the root directory.. you can manage your way down from there

Community
  • 1
  • 1
ymz
  • 6,602
  • 1
  • 20
  • 39