简体   繁体   English

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

[英]How to Properly Reference a JavaScript File in an ASP.NET Project?

I have some pages that reference javascript files. 我有一些引用javascript文件的页面。

The application exists locally in a Virtual Directory, ie 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 谢谢

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 〜/将以虚拟或其他方式告诉您应用程序的根目录

Previous answers seem to assume that the Scripts directory always exists as a subdirectory of your application root. 以前的答案似乎假设Scripts目录始终作为应用程序根目录的子目录存在。 If that assumption is correct, and if the page is also at the root, then both of your earlier tags can be simply: 如果该假设是正确的,并且如果页面也在根目录,那么您的两个早期标记可以简单地:

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

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". 据我所知,使用ResolveUrl的唯一原因是,如果应用程序中的页面位于文件夹结构中,并且标记可能出现在根目录中的页面中或“子目录”中的页面中。 If so, you can use ResolveUrl in both cases so you have an anchor point. 如果是这样,您可以在两种情况下使用ResolveUrl ,因此您有一个锚点。 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. 我从不以这种方式创作,我总是确保我知道页面在层次结构中的位置(如果需要有层次结构)并使用适当的相对路径作为当前文档。

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变量,这样任何需要知道实际应用程序根目录的脚本都可以通过这种方式访问​​它。

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. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM