adding jQuery to ASP.NET MVC application
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开发者_JS百科 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.
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.
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>
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>
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>
精彩评论