ASP。Net母版页和文件路径问题

时间:2021-11-10 15:57:51

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

我正在尝试在我的主页中添加一个jQuery脚本引用,以便它可以用于任何页面。目前看起来是这样的

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

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:

问题是路径总是相对于正在执行的aspx页面,因此只有在“jquery”的情况下才会有效。文件位于同一文件夹中。为了使它起作用,我必须把线改为:

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

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.

这显然不太理想,因为它只适用于从根文件夹向下两层的页面。如果我尝试以下操作,IIS会抛出一个关于意外字符的错误。

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

Any ideas?

什么好主意吗?

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

编辑:我也忘记提到脚本必须在head标记中

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.

当前的最高答案抛出了一个“ASP”。NET Ajax客户端框架加载失败。它是由javascript而不是。net编译器抛出的。如果我将ScriptManager移动到head部分(应该在那里),我就会得到一个关于ScriptManager需要在表单标签内的编译错误。

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

第三个答案会抛出“路径中的非法字符”,编译器除外

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

编辑2:当我将这一行添加到我的head标签时,我从IIS中得到这个错误。

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

无法修改控件集合,因为控件包含代码块(例如<%…)% >)

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

解决方案:我从下面的答案中获取经过编辑的响应,并将其放入一个asp:ContentPlaceHolder元素中

10 个解决方案

#1


106  

You could use a ScriptManager:

您可以使用ScriptManager:

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

EDIT: 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>

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

编辑2:根据评论,如果你正在观察的话

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>

#2


25  

Try <%# instead of <%= in Master page under head section

尝试<%#而不是<%=在主页的标题部分

<script type="text/javascript" 
        src="<%# ResolveUrl("~/YourScriptFolder/YourJQueryOrJavascript.js") %>">
</script>

Then in Code Behind of Master page under Page_Load Event

然后在Page_Load事件下的母版页后面的代码中

Page.Header.DataBind();

Now you are good to go with either jQuery and JavaScript as well as CSS just you need to change your path in ResolveUrl which file you want to handle CSS, JavaScript, jQuery.

现在您可以使用jQuery和JavaScript以及CSS了,只需在ResolveUrl中更改路径,您想要处理CSS、JavaScript和jQuery。

#3


9  

If you're not going to us asp:ScriptManager or absolute paths then you can do it like this:

如果你不去美国asp:ScriptManager或者绝对路径,你可以这样做:

<script runat="server" type="text/javascript" 
  src='<%= Page.ResolveUrl("~/jquery.js") %>'></script>

#4


3  

I do not know whether you guys found the solution to your problem or not. I was facing the same problem and going nuts to figure out why do I get "jQuery is undefined" error on the plugins i use. I tried all the solutions i get from the internet but no luck at all.

我不知道你们是否找到了解决你们问题的办法。我也遇到了同样的问题,我疯狂地想弄明白为什么我在使用的插件上会出现“jQuery未定义”的错误。我尝试了从互联网上得到的所有解决方案,但一点运气都没有。

But, suddenly something splash on my mind that may be the script files should be in order. So, I moved the jquery referece to first position and everything start working like charm.

但是,突然之间,我脑海中出现了一些可能是脚本文件的东西。因此,我将jquery引用移到第一个位置,然后一切开始工作,就像符咒一样。

Remember guys, if you're using any plugins with jquery, make sure you use the folloing order of setting reference to those fiels.

记住,如果你使用jquery的插件,请确保你使用了对这些fiels的引用的循环顺序。

  1. reference to the jquery library
  2. 引用jquery库
  3. reference to the other subsequent plug-in libraries and so on...
  4. 引用其他后续插件库等……

e.g.:

例如:

  1. "script src="js/jquery-1.3.2.min.js" type="text/javascript"...
  2. “脚本src = " js / jquery-1.3.2.min。js " type = " text / javascript”……
  3. "script src="js/jqDnR.min.js" type="text/javascript"...
  4. “脚本src = " js / jqDnR.min。js " type = " text / javascript”……
  5. "script src="js/jquery.jqpopup.min.js" type="text/javascript"...
  6. “脚本src = " js / jquery.jqpopup.min。js " type = " text / javascript”……
  7. "script src="js/jquery.bgiframe.min.js" type="text/javascript"...
  8. “脚本src = " js / jquery.bgiframe.min。js " type = " text / javascript”……

Always make sure you must put the jquery reference to first and then the subsequent libraries.

始终确保您必须将jquery引用放在第一个和随后的库中。

Hope, this solves your problem especially when you use with MasterPages. Its very strange that it works no matter what order you use when you don't use MasterPages but when you do, then it somehow requres the proper order.

希望这能解决您的问题,特别是当您使用母版时。非常奇怪的是,不管你使用什么顺序,当你不使用主页时,它都会工作,但当你使用它时,它会以某种方式要求正确的顺序。

Good luck and happy coding,

祝你好运,快乐编码,

Vincent D'Souza

文森特·D’索萨

#5


3  

Look at How to Run a Root “/”. This should fix all your issues regarding unresolved .js file paths. You basically reconfigure the VS Dev server to run your application as localhost:port/ as opposed to the regular localhost:port/application name/ making name resolution work the same way as it does on IIS.

看看如何运行根“/”。这将修复关于未解析的.js文件路径的所有问题。基本上,您可以重新配置VS Dev服务器,以将您的应用程序作为本地主机运行:端口/与普通本地主机相反:端口/应用程序名称/使名称解析与在IIS上执行的方式相同。

#6


0  

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

#7


0  

If this script tag goes directly to the browser, then you unlikely can substitute your site's root there. At least not on the server. So you can:

如果这个脚本标记直接进入浏览器,那么您不太可能在那里替换站点的根。至少在服务器上没有。所以你可以:

  1. Deploy site to the root of domain name and use absolute paths (simplest solution).
  2. 将站点部署到域名的根,并使用绝对路径(最简单的解决方案)。
  3. Insert this link with server control.
  4. 将此链接插入服务器控件。
  5. Preprocess resulting HTML before sending it to the client (with HttpResponse.Filter).
  6. 在将生成的HTML发送给客户端之前(使用HttpResponse.Filter)对其进行预处理。

#8


0  

You can also use <base> HTML tag:

也可以使用 HTML标签:

<base href="http://www.domain.com"></base>  

and then all the links in header section are relative to base address:

然后标题部分的所有链接都是相对于基本地址的:

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

It's often useful when you have multiple publishing destinations, like local dev web server, demo server, etc. You just replace that base URL.

当您有多个发布目的地时,比如本地dev web服务器、演示服务器等,它通常是有用的,您只需替换该基本URL。

#9


0  

<body>
<script language="javascript" src='<%= this.ResolveClientUrl("~/full/path/to/jquery.js") %>' type="text/javascript"></script>
</body>

#10


0  

For absolute path of the file for any page use it following:

对于任何页面的文件的绝对路径,请使用以下文件:

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

#1


106  

You could use a ScriptManager:

您可以使用ScriptManager:

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

EDIT: 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>

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

编辑2:根据评论,如果你正在观察的话

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>

#2


25  

Try <%# instead of <%= in Master page under head section

尝试<%#而不是<%=在主页的标题部分

<script type="text/javascript" 
        src="<%# ResolveUrl("~/YourScriptFolder/YourJQueryOrJavascript.js") %>">
</script>

Then in Code Behind of Master page under Page_Load Event

然后在Page_Load事件下的母版页后面的代码中

Page.Header.DataBind();

Now you are good to go with either jQuery and JavaScript as well as CSS just you need to change your path in ResolveUrl which file you want to handle CSS, JavaScript, jQuery.

现在您可以使用jQuery和JavaScript以及CSS了,只需在ResolveUrl中更改路径,您想要处理CSS、JavaScript和jQuery。

#3


9  

If you're not going to us asp:ScriptManager or absolute paths then you can do it like this:

如果你不去美国asp:ScriptManager或者绝对路径,你可以这样做:

<script runat="server" type="text/javascript" 
  src='<%= Page.ResolveUrl("~/jquery.js") %>'></script>

#4


3  

I do not know whether you guys found the solution to your problem or not. I was facing the same problem and going nuts to figure out why do I get "jQuery is undefined" error on the plugins i use. I tried all the solutions i get from the internet but no luck at all.

我不知道你们是否找到了解决你们问题的办法。我也遇到了同样的问题,我疯狂地想弄明白为什么我在使用的插件上会出现“jQuery未定义”的错误。我尝试了从互联网上得到的所有解决方案,但一点运气都没有。

But, suddenly something splash on my mind that may be the script files should be in order. So, I moved the jquery referece to first position and everything start working like charm.

但是,突然之间,我脑海中出现了一些可能是脚本文件的东西。因此,我将jquery引用移到第一个位置,然后一切开始工作,就像符咒一样。

Remember guys, if you're using any plugins with jquery, make sure you use the folloing order of setting reference to those fiels.

记住,如果你使用jquery的插件,请确保你使用了对这些fiels的引用的循环顺序。

  1. reference to the jquery library
  2. 引用jquery库
  3. reference to the other subsequent plug-in libraries and so on...
  4. 引用其他后续插件库等……

e.g.:

例如:

  1. "script src="js/jquery-1.3.2.min.js" type="text/javascript"...
  2. “脚本src = " js / jquery-1.3.2.min。js " type = " text / javascript”……
  3. "script src="js/jqDnR.min.js" type="text/javascript"...
  4. “脚本src = " js / jqDnR.min。js " type = " text / javascript”……
  5. "script src="js/jquery.jqpopup.min.js" type="text/javascript"...
  6. “脚本src = " js / jquery.jqpopup.min。js " type = " text / javascript”……
  7. "script src="js/jquery.bgiframe.min.js" type="text/javascript"...
  8. “脚本src = " js / jquery.bgiframe.min。js " type = " text / javascript”……

Always make sure you must put the jquery reference to first and then the subsequent libraries.

始终确保您必须将jquery引用放在第一个和随后的库中。

Hope, this solves your problem especially when you use with MasterPages. Its very strange that it works no matter what order you use when you don't use MasterPages but when you do, then it somehow requres the proper order.

希望这能解决您的问题,特别是当您使用母版时。非常奇怪的是,不管你使用什么顺序,当你不使用主页时,它都会工作,但当你使用它时,它会以某种方式要求正确的顺序。

Good luck and happy coding,

祝你好运,快乐编码,

Vincent D'Souza

文森特·D’索萨

#5


3  

Look at How to Run a Root “/”. This should fix all your issues regarding unresolved .js file paths. You basically reconfigure the VS Dev server to run your application as localhost:port/ as opposed to the regular localhost:port/application name/ making name resolution work the same way as it does on IIS.

看看如何运行根“/”。这将修复关于未解析的.js文件路径的所有问题。基本上,您可以重新配置VS Dev服务器,以将您的应用程序作为本地主机运行:端口/与普通本地主机相反:端口/应用程序名称/使名称解析与在IIS上执行的方式相同。

#6


0  

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

#7


0  

If this script tag goes directly to the browser, then you unlikely can substitute your site's root there. At least not on the server. So you can:

如果这个脚本标记直接进入浏览器,那么您不太可能在那里替换站点的根。至少在服务器上没有。所以你可以:

  1. Deploy site to the root of domain name and use absolute paths (simplest solution).
  2. 将站点部署到域名的根,并使用绝对路径(最简单的解决方案)。
  3. Insert this link with server control.
  4. 将此链接插入服务器控件。
  5. Preprocess resulting HTML before sending it to the client (with HttpResponse.Filter).
  6. 在将生成的HTML发送给客户端之前(使用HttpResponse.Filter)对其进行预处理。

#8


0  

You can also use <base> HTML tag:

也可以使用 HTML标签:

<base href="http://www.domain.com"></base>  

and then all the links in header section are relative to base address:

然后标题部分的所有链接都是相对于基本地址的:

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

It's often useful when you have multiple publishing destinations, like local dev web server, demo server, etc. You just replace that base URL.

当您有多个发布目的地时,比如本地dev web服务器、演示服务器等,它通常是有用的,您只需替换该基本URL。

#9


0  

<body>
<script language="javascript" src='<%= this.ResolveClientUrl("~/full/path/to/jquery.js") %>' type="text/javascript"></script>
</body>

#10


0  

For absolute path of the file for any page use it following:

对于任何页面的文件的绝对路径,请使用以下文件:

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