问题描述
在我使用 MasterPage
和 Routing
的 asp.net 网站中,我在 link
的 href
属性中使用波浪号MasterPage 的 head
部分中样式表的 code> 标记.像这样:
In my asp.net website using MasterPage
and Routing
I use a tilde in the href
attribute of the link
tag for the stylesheet in the head
section of the MasterPage. Like this:
<link href="~/Styles/Main.css" rel="stylesheet" type="text/css" />
这就像一个魅力.由于网站使用路由,因此 url 将包含越来越多的 /
,但样式表的 href 仍然有效,因为波浪号指向 Web 应用程序的根并使用样式.
Which works like a charm. Since the website uses routing the url will contain more and more /
, yet the stylesheet's href remains valid because the tilde points to the root of the web application and the styles are used.
我尝试对 script
标签的 src
属性使用相同的技术,但这似乎没有产生预期的结果.我试过了:
I tried using the same technique for the src
attribute of the script
tags, but this doesn't seem to produce the expected result. I tried:
<script src="~/Scripts/jquery-1.8.2.min.js" type="text/javascript" ></script>
但这只是将波浪号字符输出到页面的 HTML 中,而不是像对 href
属性那样用 Web 应用程序的根替换它.我的经验是 asp.net 替换 href
属性中的波浪号,而不是 src
属性.
But this just outputs the tilde character to the HTML of the page instead of replacing it with the root of the web application as it does for the href
attribute. My experience is that asp.net replaces tilde in href
attributes but not in src
attributes.
如何使波浪号在脚本标签的 src
属性中起作用?
How can I make the tilde work in the src
atrribute of script tags?
推荐答案
我不确定有没有办法让它在没有一点帮助的情况下正常工作.这应该有效,但不如 link
好:
I'm not sure there is a way to get it to work correctly without a bit of assistance. This should work, not as nice as the link
though:
<script src="<%=ResolveUrl("~/Scripts/jquery-1.8.2.min.js")%>"></script>
这篇关于在脚本标签 src 属性中使用波浪号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!