在script标签src属性中使用tilde

时间:2022-12-06 10:54:44

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:

在我使用MasterPage和Routing的asp.net网站中,我在MasterPage的head部分中的样式表的链接标记的href属性中使用了波浪号。喜欢这个:

<link href="~/Styles/Main.css" rel="stylesheet" type="text/css" />

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.

这就像一个魅力。由于网站使用路由,网址将包含越来越多的/,但样式表的href仍然有效,因为代字号指向Web应用程序的根目录并使用样式。

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:

我尝试对脚本标记的src属性使用相同的技术,但这似乎不会产生预期的结果。我试过了:

<script src="~/Scripts/jquery-1.8.2.min.js" type="text/javascript" ></script>

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.

但这只是将波浪号字符输出到页面的HTML而不是像使用href属性那样用Web应用程序的根替换它。我的经验是asp.net替换了href属性中的波浪号,但没有替换src属性中的波浪号。

How can I make the tilde work in the src atrribute of script tags?

如何在代码脚本标签的src中使代字号工作?

1 个解决方案

#1


12  

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>

#1


12  

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>

相关文章