ASP.Net母版页和文件路径问题 [英] ASP.Net Master Page and File path issues

查看:120
本文介绍了ASP.Net母版页和文件路径问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的母版页添加脚本参考jQuery的,这样它会为任何网页的工作。目前,它看起来像这样

I'm trying to add a script reference to jQuery in my master page so that it will work for any page. It currently looks like this

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

问题是,路径总是相对于执行aspx页面所以如果的jquery.js文件位于同一文件夹中,这只会工作。为了使它工作,我不得不行更改为:

The problem is that the path is always relative to the executing aspx page so this will only work if the "jquery.js" file is located in the same folder. To make it work I have to change the line to:

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

这显然是不理想的,因为它只会为深两个层次从根文件夹页工作。如果我尝试以下方法,IIS将引发约一个意外的字符错误。

This is obviously less than ideal because it will only work for pages that are two levels deep from the root folder. If I try the following, IIS throws an error about an unexpected character.

<script runat="server" type="text/javascript" src="~/jquery.js"></script>

任何想法?

编辑:我忘了提,以及脚本必须在头标记

I forgot to mention as well that the script MUST be in the head tag

目前最多的回答抛出一个的 ASP.NET AJAX客户端框架加载失败。的当我把它添加到我的母版页错误。它从JavaScript,而不是.net编译抛出。如果我移动的ScriptManager的头部部分,在那里它应该是我得到一个编译错误有关的ScriptManager需要成为一个窗体标记内。

The current top answer throws a "ASP.NET Ajax client-side framework failed to load." error when I add it to my master page. Its thrown from javascript and not the .Net compiler. If I move the ScriptManager to the head section where it should be I get a compile error about the ScriptManager needing to be inside a form tag.

第三个答案抛出一个的路径中具有非法字符。的从编译器异常

The third answer throws a "Illegal characters in path." exception from the compiler

编辑2::当我加入这一行到我头上的标签我从IIS这个错误

EDIT 2: When I add that line to my head tag I get this error from IIS.

Controls集合不能修改,因为该控件包含code块(即&LT;%...%>)

解决:我把从下面的答案编辑的响应,并把它的里面的 ASP:的ContentPlaceHolder 的元素

SOLVED: I took the edited response from the answer below and put it inside an asp:ContentPlaceHolder element

推荐答案

您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.aspx\"><$c$c>ScriptManager:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/jquery.js" />
    </Scripts>
</asp:ScriptManager>

编辑:如果您的绝对的需要这个在&LT; HEAD&GT; 部分,你可以这样做

If you absolutely need this in your <head> section, you could do something like:

<head>
    <script type="text/javascript" 
        src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
</head>

编辑2:根据该意见,如果你观察到

EDIT 2: According to the comments, if you are observing that

Controls集合不能修改,因为该控件包含code块(即&LT;%...%>)

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

您可能需要更改上述使用数据绑定语法:

you may need to change the above to use the data-binding syntax:

<head>
    <script type="text/javascript" 
        src="<%# Page.ResolveClientUrl("~/jquery.js") %>"></script>
</head>

这篇关于ASP.Net母版页和文件路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆