views:

60

answers:

4

I've a problem with adding jQuery to an ASP.NET MVC application. I add jquery to Site.Master like that:

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

In Visual Studio 2010 this is o.k. - now I publish a application to a web server (IIS 7) an a Folder like:

h ttp://localhost/AnApplication 

When calling the site I see a 404 - File Not found in FireBug Net - View. FireBug shows that the Application is looking for:

h ttp://localhost/Scripts/jquery-1.4.1.js

But file would be at h ttp://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 404 Error in 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 without success.

Thanks for your help. Tobi

A: 

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

<script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
Jappie
Thanks, is there no way to do it relative? It is not in my hands to set the path of the application - it may change. I can also set the path for css files relative - that also works - why does it not work with js? link href="../../Content/Site.css" rel="stylesheet" type="text/css"
PirzerTobias
updated the answer to use path absolute to home directory
Jappie
@Jappie - which was my original answer. =)
RPM1984
doesnt work in MVC
Stefanvds
yes, that is exactly my problem - src="~/Scripts/jquery-1.4.1.js" does not link to /AnApplication/Scripts/jquery.js, but /Scripts/jquery.js. :-/
PirzerTobias
A: 

Try not to use relative paths for includes.

Do it like this:

<script src="~/AnApplication/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
RPM1984
doesnt work in MVC
Stefanvds
But I dont know the Path the Application will get ...
PirzerTobias
+3  A: 

guys, this is MVC, it doesnt work like that!

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

this is how you do it.

Stefanvds
Ahhh now i see. I totally forgot this was MVC, my bad =) +1
RPM1984
Muchas Gracias - works perfect ... that easy question is not answered in my mvc-book ...
PirzerTobias
what's wrong with just writing :<script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
Avi Pinto
always start from the root and go to the scripts library
Avi Pinto
+1  A: 

Try an absolute path to some CDN like <script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;

abatishchev
that's a good suggestion but it doesn't fix his problem if he wants for example to display an image which is in the download folder :)
Stefanvds