Adding jQuery to an ASP.NET MVC Application

I have a problem adding jQuery to an ASP.NET MVC application. I am adding jquery to Site.Master:

<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>

In Visual Studio 2010, this is ok - now I publish the application on a web server (IIS 7), for example in a folder:

http://localhost/AnApplication 

When I call the site, I see a 404 file - File not found in FireBug Net - View. FireBug shows that the application is looking for:

http://localhost/Scripts/jquery-1.4.1.js

But the file will be http: //localhost/AnApplication/Scripts/jquery-1.4.1.js

How can I reference the jquery.js file that asp.net finds the jQuery file without generating a 404 error in the IIS log file?

I tried with <script src="../Scripts/jquery-1.4.1.js" type="text/javascript" />and <script src="~/Scripts/jquery-1.4.1.js" type="text/javascript" />, but to no avail.

+3
source share
4 answers

, MVC, !

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

.

+4

CDN, <script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

+1

Depending on which controller and action is currently being invoked, the relative path changes. You can link to your js files with an absolute path from the application directory:

<script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
0
source

Do not try to use relative paths for inclusions.

Do it like this:

<script src="~/AnApplication/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
0
source

Source: https://habr.com/ru/post/1765586/


All Articles