3

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:

http://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:

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

But file would be at 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 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.

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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

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"></script>

1 Comment

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 :)
0

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>

3 Comments

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"
which was my original answer. =)
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. :-/
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.