jQuery 库位置希望在服务器上以不同方式引用 [英] jQuery library location wants to be referenced differently on server
问题描述
我有一个使用 jQuery 的 ASP.NET MVC 应用程序.为了加载 js 库,我像这样引用它们:
I've got an ASP.NET MVC application that uses jQuery. To load the js libraries, I reference them like this:
<script type="text/javascript" src="../../Scripts/jquery-1.3.2.min.js"></script>
这在本地工作正常,但是当我将其发布到服务器时,它找不到库.为了让它工作,我必须把它改成这样:
This works fine locally, but when I publish it to the server, it can't find the library. To get it to work, I have to change it to this:
<script type="text/javascript" src="../Scripts/jquery-1.3.2.min.js"></script>
进行此更改允许它在本地和服务器上工作,但它给了我一个未找到文件的警告.所以我有几个问题:
Making this change allows it to work locally and on the server, but it gives me a warning that the file was not found. So I've got a couple of questions:
- 如果我没有引用正确的库位置,代码如何在本地工作?
- 我可以为可以在任何地方工作且不会发出警告的脚本路径添加什么?
我知道 #2 的答案是将库存储在某个已知的绝对路径中,但我还没有准备好承诺.
I know an answer to #2 is to store the libraries in some known absolute path, but I'm not ready to commit to that yet.
推荐答案
试试这个:
<script type="text/javascript" src='<%= Url.Content("~/Scripts/jquery-1.3.2.min.js") %>'></script>
这将相对化应用程序根目录的路径,无论它是在顶层还是在虚拟目录中.我实际上开发了一个 HtmlHelper 扩展,让我们清理它:
This will relativize the path to the root of your application regardless of whether it is at the top level or in a virtual directory. I actually developed a HtmlHelper extension that lets clean this up to:
<%= Html.Javascript( Url.Content( "~/Scripts/jquery-1.3.2.min.js" )) %>
添加以下内容以获得智能感知.这需要相对路径才能工作,但在运行时被排除在外,因为条件(总是)失败.
Add the following to get intellisense. This needs the relative path to work, but gets excluded at runtime because the condition (always) fails.
<% if (false) { %>
<script type="text/javascript" src="../../Scripts/jquery-1.3.2.vsdoc.js"></script>
<% } %>
这篇关于jQuery 库位置希望在服务器上以不同方式引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!