.NET基HREF&安培;在子文件夹的样式表链接 [英] .NET base href & stylesheet links in subfolder

查看:100
本文介绍了.NET基HREF&安培;在子文件夹的样式表链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我会使用基本href 标签来让我相对URL不同的安装工作。

在我的母版页,我有:

 <基地HREF =HTTP://本地主机/ myproject的//>
<链接rel =stylesheet属性类型=文/ CSS=服务器的href =CSS /的main.css/>
<脚本类型=文/ JavaScript的SRC =JS / jquery.min.js>< / SCRIPT>

这是当我在 HTTP工作正常://localhost/myproject/default.aspx ,CSS的路径解析精细,如: HTTP://localhost/myproject/css/main.css

但后来我有一个子文件夹,报告。当我浏览到 HTTP://localhost/myproject/reports/default.aspx ,并查看源代码,这是我所看到的:

 <基地HREF =HTTP://本地主机/ myproject的//>
<链接rel =stylesheet属性类型=文/ CSS的href =../ CSS /的main.css/>
<脚本类型=文/ JavaScript的SRC =JS / jquery.min.js>< / SCRIPT>

东西注入了 ../ 的href 链接的属性标签,这意味着它解决为: HTTP://localhost/css/main.css 未找到

这是奇怪的,因为它的工作是我期望的脚本标签;只是链接标记得到这个注入。我在IE浏览器和放大器查看源; Chrome和两者都是一样的,所以我认为这是IIS / .NET是在干什么,但他们真的不应该触及HTML,因为它不是 =服务器。与此有点玩,我看到母版页上的链接标签被修改,但不是脚本标签(如果他们是相对URL没有前导/)。我认为是因为他们SRC属性,而不是HREF属性。因此,有/不具有基本href 将修复一个类型,但攻破对方。

我是不是快要疯了,这是一个错误,还是我抱着它错了吗?

编辑:
我实际上没有硬编码的基本标记的href。我写了一个被加载到内存的应用程序从数据库表中设置的值。因此,它实际上做:<基本href =<%= Settings.Configuration.website_rooturl%GT; /> 在我的开发机器上的值可以是的http://本地主机/ 的http://本地主机/项目/ 和一个客户的网站是 http://www.clientsite.com/ 和另一个客户端的网站它是一个虚拟目录 http://www.clientsite2.com/project/


解决方案

 <链接HREF =<%= ResolveClientUrl(〜/ CSS /的main.css)%&GT ;相对=样式类型=文/ CSS/>

I thought I'd use the base href tag to make my relative URLs work on different installations.

In my master page, I have:

<base href="http://localhost/myproject/" />
<link rel="stylesheet" type="text/css" runat="server" href="css/main.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>

That works fine when I'm on http://localhost/myproject/default.aspx, the css path resolves fine, such as: http://localhost/myproject/css/main.css

But then I have a subfolder, reports. When I navigate to http://localhost/myproject/reports/default.aspx, and view source, this is what I see:

<base href="http://localhost/myproject/" />
<link rel="stylesheet" type="text/css" href="../css/main.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>

Something has injected the ../ into the href attribute of the link tag, which means it's resolving as: http://localhost/css/main.css which is not found.

This is odd, because it's working as I'd expect for the script tag; just the link tag gets this injection. I viewed source in IE & Chrome and both were the same, so I assume it's IIS/.NET that's doing it, but they shouldn't really touch the HTML because it's not runat="server". Playing with this a little, I see the link tags on the Master page are modified, but not the script tags (if they're relative URLs without the leading "/"). I assume that's because they're "src" attributes and not "href" attributes. So having / not having the base href will fix the one type but break the other.

Am I going crazy, is this a bug, or am I holding it wrong?

EDIT: I'm not actually hard-coding the href of the base tag. I'm writing out the value of a setting that is loaded to application memory from a database table. So it's actually doing: <base href="<%=Settings.Configuration.website_rooturl%>" /> where on my dev machine the value could be http://localhost/ or http://localhost/project/ and on one client site it's http://www.clientsite.com/ and another client site it's a virtual directory http://www.clientsite2.com/project/.

解决方案

<link href="<%= ResolveClientUrl("~/css/main.css") %>" rel="stylesheet" type="text/css" />

这篇关于.NET基HREF&安培;在子文件夹的样式表链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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