【发布时间】:2014-01-05 15:39:19
【问题描述】:
我正在用 C# 在 VS 2012,.NET 框架 4.5 中构建一个 ASP.NET Webform 应用程序
我在应用程序的根目录中有一个 MasterPage,JavaScript 文件位于名为 js 的文件夹中。
问题出在:如果页面位于根文件夹中,那么一切都工作正常(css + js),如果我将任何页面放在子文件夹中 然后 css 可以工作,但是那些 JavaScripts 根本不工作,显然引用路径是错误的。
所以 Css 引用路径很好,但是对于脚本,无论我使用什么,它们都不起作用(js/script.js 或 ~/js/script.js 或 /js/script.js 或 ../ ResolveUrl, ResolClientveUrl ... 或 http://yobriefca.se/blog/2010/10/19/%3C-head-%3Eache-including-javascript-in-asp-dot-net-master-pages/ 中的所有方法)它们都指的是 root/SUB-FOLDER/js/script.js 而不是 root/js/script.js
在根目录中:单个 MasterPage、Default.aspx、test.aspx、js 文件夹、css 文件夹和 Pages 文件夹。默认和测试页面是工作文件,但是 Pages 文件夹中的所有页面都没有显示 .js 所以当页面不在根目录中时路径是错误的
在我的母版页中:
<head runat="server">
<title></title>
<link rel="stylesheet" href="~/css/style.css" />
<%-- tried these and lot more but NOT workkkkkkkkkkk --%>
<%--<script src="~/js/jquery-1.7.1.min.js" ></script>
<script src="~/js/script.js" ></script>--%>
<%--<script src='<%=ResolveUrl("~/js/jquery-1.7.1.min.js") %>' ></script>
<script src='<%=ResolveUrl("~/js/script.js") %>' type="text/javascript"></script>--%>
<%--<script src='<%=ResolveClientUrl("~/js/jquery-1.7.1.min.js") %>' type="text/javascript"></script>
<script src='<%=ResolveClientUrl("~/js/script.js") %>' type="text/javascript"></script>--%>
<asp:ContentPlaceHolder ID="Head" runat="server">
</asp:ContentPlaceHolder>
script.js 类似于:
....
$.include('js/superfish.js')
$.include('js/FF-cash.js')
$.include('js/tms-0.4.x.js')
$.include('js/uCarausel.js')
$.include('js/jquery.easing.1.3.js')
$.include('js/jquery.tools.min.js')
$.include('js/jquery.jqtransform.js')
$.include('js/jquery.quicksand.js')
$.include('js/jquery.snippet.min.js')
$.include('js/jquery-ui-1.8.17.custom.min.js')
$.include('js/jquery.cycle.all.min.js')
$.include('js/jquery.cookie.js')
$(function(){
if($('.tweet').length)$.include('js/jquery.tweet.js');
if($('.lightbox-image').length)$.include('js/jquery.prettyPhoto.js');
if($('#contact-form').length||$('#contact-form2').length)$.include('js/forms.js');
if($('.kwicks').length)$.include('js/kwicks-1.5.1.pack.js');
if($('#counter').length)$.include('js/jquery.countdown.js');
if($('.fixedtip').length||$('.clicktip').length||$('.normaltip').length)$.include('js/jquery.atooltip.pack.js')
// Slider
$('.main-slider')._TMS({
.....
Web 浏览器的开发者工具(控制台)出现错误:
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/tms-0.4.x.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/uCarausel.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.jqtransform.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.quicksand.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.snippet.min.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/FF-cash.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/superfish.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.tools.min.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery-ui-1.8.17.custom.min.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.cycle.all.min.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.easing.1.3.js
Failed to load resource: the server responded with a status of 404 (Not Found) http://ApplicationRoot/Pages/js/jquery.cookie.js
Uncaught TypeError: Object [object Object] has no method '_TMS' script.js:22
event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
【问题讨论】:
-
您应该删除
~/。由于脚本不是 ASP.NET 控件,所以按原样添加到最终的 html 中,浏览器无法识别~/。 -
@Givi 不起作用,我尝试了所有方法 /js、../js、~/js 或 js,然后在 ResolveUrl 和 ResolClientveUrl 中相同
-
移除服务器端评论标签。
<%-- --%> -
@Givi 已删除 并且根文件夹中包含的所有页面都不起作用
-
你能展示你的服务器的结构吗?并展开母版页的结构?
标签: c# javascript jquery asp.net css