如何在ASP.NET项目中正确引用JavaScript文件?

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

I have some pages that reference javascript files.

我有一些引用javascript文件的页面。

The application exists locally in a Virtual Directory, i.e. http://localhost/MyVirtualDirectory/MyPage.aspx

该应用程序存在于虚拟目录中,即http://localhost/MyVirtualDirectory/MyPage.aspx

so locally I reference the files as follows:

所以本地我引用文件如下:

<script src="/MyVirtualDirectory/Scripts/MyScript.js" type="text/javascript"></script>

The production setup is different though. The application exists as its own web site in production, so I don't need to include the reference to the virtual directory. The problem with this is that I need to modify every file that contains a javascript reference so it looks like the following:

但是生产设置不同。该应用程序在生产中作为自己的网站存在,因此我不需要包含对虚拟目录的引用。这个问题是我需要修改包含javascript引用的每个文件,所以它看起来如下所示:

<script src="../Scripts/MyScript.js" type="text/javascript"></script>

I've tried referencing the files this way in my local setup but it doesn't work.

我尝试在我的本地设置中以这种方式引用文件,但它不起作用。

Am I going about this completely wrong? Can somebody tell me what I need to do?

我完全错了吗?有人可以告诉我我需要做什么吗?

Thanks

4 个解决方案

#1


3  

Previous answers seem to assume that the Scripts directory always exists as a subdirectory of your application root. If that assumption is correct, and if the page is also at the root, then both of your earlier tags can be simply:

以前的答案似乎假设Scripts目录始终作为应用程序根目录的子目录存在。如果这个假设是正确的,并且如果页面也在根目录下,那么您的两个早期标记可以简单地:

<script src="Scripts/MyScript.js" type="text/javascript"></script>

But my read of your second example is that Scripts isn't always a subdirectory of your application root (because the ../ at the beginning moves up a level, so Scripts would be a peer of your application root). That said, you did say the second example didn't work. :-) But if that's really the case, I'd strongly recommend adjusting one environment or the other so that the relative paths agree, and then always using relative paths as above.

但是我读到的第二个例子是Scripts并不总是应用程序根目录的子目录(因为开头的../会提升一个级别,因此Scripts将成为应用程序根目录的对等体)。也就是说,你确实说第二个例子不起作用。 :-)但如果确实如此,我强烈建议调整一个环境或另一个环境以使相对路径一致,然后始终使用上面的相对路径。

The only reason for using ResolveUrl as far as I know would be if the pages in the application are in a folder structure and the tag may appear in a page at the root or in a page in a "subdirectory". If so, you can use ResolveUrl in both cases so you have an anchor point. I never author things that way, I always ensure I know where the page will be in the hierarchy (if there needs to be a hierarchy) and use an appropriate relative path for the current document.

据我所知,使用ResolveUrl的唯一原因是应用程序中的页面是否在文件夹结构中,并且标记可能出现在根目录中的页面中或“子目录”中的页面中。如果是这样,您可以在两种情况下使用ResolveUrl,因此您有一个锚点。我从不以这种方式创作,我始终确保我知道页面在层次结构中的位置(如果需要有层次结构)并使用适当的相对路径作为当前文档。

#2


19  

Use ResolveUrl("~/")

<script src="<%=ResolveUrl("~/scripts/myscript.js") %>" type="text/javascript"></script>

~/ will get to you the root of your application, virtual or otherwise

〜/将以虚拟或其他方式告诉您应用程序的根目录

#3


0  

You can use the HttpRuntime.AppDomainAppVirtualPath property to get the virtual path (if any) for your app and use that to fix up the javascript paths (using <%= ... %> in the <script> tags etc.)

您可以使用HttpRuntime.AppDomainAppVirtualPath属性获取应用程序的虚拟路径(如果有),并使用该路径修复javascript路径(使用

You can further add a global javascript variable in your master page that exposes that value as well, so that any scripts that need to know the actual app root can access it that way.

您可以在主页面中进一步添加一个公开该值的全局javascript变量,这样任何需要知道实际应用程序根目录的脚本都可以通过这种方式访问​​它。

#4


-1  

Another way in MVC5:

MVC5的另一种方式:

1) in the layout html View file, place RenderSection in the place you need the script to be:

1)在布局html视图文件中,将RenderSection放在需要脚本的位置:

<html><body>
@RenderSection("scripts1", required: false)
</body></html>

note that you can change the "Scripts1" to be whatever name you like.

请注意,您可以将“Scripts1”更改为您喜欢的任何名称。

2) in your view html file, just call the "scripts1", doesn't matter where, with your path and js file name:

2)在你的html文件视图中,只需调用“scripts1”,无论在哪里,用你的路径和js文件名:

@Scripts1.Render("~/Scripts/MyScript.js")

3) make sure the MyScript js file is in the Scripts Folder of your project.

3)确保MyScript js文件位于项目的Scripts文件夹中。

That's it.

#1


3  

Previous answers seem to assume that the Scripts directory always exists as a subdirectory of your application root. If that assumption is correct, and if the page is also at the root, then both of your earlier tags can be simply:

以前的答案似乎假设Scripts目录始终作为应用程序根目录的子目录存在。如果这个假设是正确的,并且如果页面也在根目录下,那么您的两个早期标记可以简单地:

<script src="Scripts/MyScript.js" type="text/javascript"></script>

But my read of your second example is that Scripts isn't always a subdirectory of your application root (because the ../ at the beginning moves up a level, so Scripts would be a peer of your application root). That said, you did say the second example didn't work. :-) But if that's really the case, I'd strongly recommend adjusting one environment or the other so that the relative paths agree, and then always using relative paths as above.

但是我读到的第二个例子是Scripts并不总是应用程序根目录的子目录(因为开头的../会提升一个级别,因此Scripts将成为应用程序根目录的对等体)。也就是说,你确实说第二个例子不起作用。 :-)但如果确实如此,我强烈建议调整一个环境或另一个环境以使相对路径一致,然后始终使用上面的相对路径。

The only reason for using ResolveUrl as far as I know would be if the pages in the application are in a folder structure and the tag may appear in a page at the root or in a page in a "subdirectory". If so, you can use ResolveUrl in both cases so you have an anchor point. I never author things that way, I always ensure I know where the page will be in the hierarchy (if there needs to be a hierarchy) and use an appropriate relative path for the current document.

据我所知,使用ResolveUrl的唯一原因是应用程序中的页面是否在文件夹结构中,并且标记可能出现在根目录中的页面中或“子目录”中的页面中。如果是这样,您可以在两种情况下使用ResolveUrl,因此您有一个锚点。我从不以这种方式创作,我始终确保我知道页面在层次结构中的位置(如果需要有层次结构)并使用适当的相对路径作为当前文档。

#2


19  

Use ResolveUrl("~/")

<script src="<%=ResolveUrl("~/scripts/myscript.js") %>" type="text/javascript"></script>

~/ will get to you the root of your application, virtual or otherwise

〜/将以虚拟或其他方式告诉您应用程序的根目录

#3


0  

You can use the HttpRuntime.AppDomainAppVirtualPath property to get the virtual path (if any) for your app and use that to fix up the javascript paths (using <%= ... %> in the <script> tags etc.)

您可以使用HttpRuntime.AppDomainAppVirtualPath属性获取应用程序的虚拟路径(如果有),并使用该路径修复javascript路径(使用

You can further add a global javascript variable in your master page that exposes that value as well, so that any scripts that need to know the actual app root can access it that way.

您可以在主页面中进一步添加一个公开该值的全局javascript变量,这样任何需要知道实际应用程序根目录的脚本都可以通过这种方式访问​​它。

#4


-1  

Another way in MVC5:

MVC5的另一种方式:

1) in the layout html View file, place RenderSection in the place you need the script to be:

1)在布局html视图文件中,将RenderSection放在需要脚本的位置:

<html><body>
@RenderSection("scripts1", required: false)
</body></html>

note that you can change the "Scripts1" to be whatever name you like.

请注意,您可以将“Scripts1”更改为您喜欢的任何名称。

2) in your view html file, just call the "scripts1", doesn't matter where, with your path and js file name:

2)在你的html文件视图中,只需调用“scripts1”,无论在哪里,用你的路径和js文件名:

@Scripts1.Render("~/Scripts/MyScript.js")

3) make sure the MyScript js file is in the Scripts Folder of your project.

3)确保MyScript js文件位于项目的Scripts文件夹中。

That's it.