我如何从ASP.NET MVC项目中引用c#库项目中的javascript文件?

时间:2022-09-09 01:44:27

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.

这会在ASP.NET .cshtml页面所在的同一项目的“Scripts / Controls”文件夹中找到SomeScript.js。

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

现在是时候在两个项目之间共享javascript文件了。我需要把它移到图书馆项目。

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?

在同一个解决方案中,src属性的值必须在一个完全不同的项目中找到javascript文件?我还需要做别的吗?这个问题通常如何解决?

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.

我在“SomeScript.js”属性页面上尝试了“复制到输出目录”,这会将脚本及其包含的文件夹复制到“bin”目录中。但是,当尝试访问bin文件夹中的.js文件时,chrome会报告“不允许加载本地资源”。

2 个解决方案

#1


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.

脚本所在的文件夹必须位于项目树中,尤其是在您计划将应用程序发布到远程服务器时。 Visual Studio提供了“添加现有项目”作为链接的选项,允许您将项目树外部存储的项目符号链接到项目树中的文件夹中。对原始文件的任何更改都将反映在您的项目中;但是如果文件被删除,链接将保持不变。

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

您还必须在项目关闭标记之前将以下内容添加到项目.csproj文件的末尾。

<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.

对.csproj文件的这种编辑将使visual studio实际上将链接文件复制到构建结束时链接的位置。

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

这样,您仍然可以在项目中引用〜/ Scripts / Controls / SomeScript.js。

#2


1  

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

使用此引用来解决您的问题:Server.MapPath(“。”),Server.MapPath(“〜”),Server.MapPath(@“\”),Server.MapPath(“/”)。有什么不同?

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

似乎最好的方法是在开头使用/ - 这是根目录..你可以从那里管理你的方式

#1


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.

脚本所在的文件夹必须位于项目树中,尤其是在您计划将应用程序发布到远程服务器时。 Visual Studio提供了“添加现有项目”作为链接的选项,允许您将项目树外部存储的项目符号链接到项目树中的文件夹中。对原始文件的任何更改都将反映在您的项目中;但是如果文件被删除,链接将保持不变。

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

您还必须在项目关闭标记之前将以下内容添加到项目.csproj文件的末尾。

<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.

对.csproj文件的这种编辑将使visual studio实际上将链接文件复制到构建结束时链接的位置。

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

这样,您仍然可以在项目中引用〜/ Scripts / Controls / SomeScript.js。

#2


1  

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

使用此引用来解决您的问题:Server.MapPath(“。”),Server.MapPath(“〜”),Server.MapPath(@“\”),Server.MapPath(“/”)。有什么不同?

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

似乎最好的方法是在开头使用/ - 这是根目录..你可以从那里管理你的方式