如何在 ASP.NET 项目中正确引用 JavaScript 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2509508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Properly Reference a JavaScript File in an ASP.NET Project?
提问by DaveDev
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
谢谢
采纳答案by T.J. Crowder
Previous answersseem to assume that the Scriptsdirectory 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 Scriptsisn'talways a subdirectory of your application root (because the ../at the beginning moves up a level, so Scriptswould 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 ResolveUrlas 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 ResolveUrlin 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在这两种情况下使用,这样你就有了一个锚点。我从不那样创作,我总是确保我知道页面在层次结构中的位置(如果需要层次结构)并为当前文档使用适当的相对路径。
回答by hunter
Use ResolveUrl("~/")
使用 ResolveUrl("~/")
<script src="<%=ResolveUrl("~/scripts/myscript.js") %>" type="text/javascript"></script>
~/ will get to you the root of your application, virtual or otherwise
~/ 将获得应用程序的根目录,无论是虚拟的还是其他的
回答by technophile
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 路径(在 <script> 标签等中使用 <%= ... %> )
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 变量来公开该值,以便任何需要知道实际应用根目录的脚本都可以通过这种方式访问它。
回答by Dudi
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.
就是这样。

