Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

asp.net mvc - jQuery library location wants to be referenced differently on server

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:

  1. How does the code work locally if I'm not referencing the correct library location?
  2. What can I put for a script path that will work everywhere and not throw a warning?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

<script type="text/javascript" src='<%= Url.Content("~/Scripts/jquery-1.3.2.min.js") %>'></script>

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>
<% } %>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...