母版CSS链接无法解析〜 [英] Masterpage CSS link can't resolve ~

查看:157
本文介绍了母版CSS链接无法解析〜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在C#中ASP.net的Web界面。如果它的事项,我使用XHTML 1.0过渡。

I'm working on a web interface in C# ASP.net. If it matters, I'm using XHTML 1.0 Transitional.

该网站有一个母版的东西去,而这也正是问题的走了进来。当我用一个真正的绝对路径标头中的CSS链接,一切都很好。但后来当我试图将它转换为波浪线符号,所有的造型打破了。

This website has a masterpage thing going, and that's where the problem came in. When I used a real absolute path for the CSS link in the header, everything was fine. But then when I tried to switch it to tilde notation, all the styling broke.

下面是原来的母版页,这工作得很好的一个片段:

Here's a fragment of the original master page, which worked fine:

<head>
    <title>Account Information</title>
    <link href="/css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
...
</body>

但后来我们发现这个账户的事情将是一个不生活在服务器的根目录的应用程序,所以我们必须做出改变。

But then we found out that this account thing is going to be an application that doesn't live on the server root, so we had to make changes.

<head>
    <title>Account Information</title>
    <link runat="server" href="~/css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
...
</body>

现在,这些相同的更改(添加=服务器和一个波浪线)工作就好了其他地方的页面,但是这一次却没有。当我看着输出,它不是解决波浪线,因此在myserver.net/~/css/main.css,这显然是行不通的链接实际上是指向。

Now, those same changes (adding runat="server" and a tilde) worked just FINE everywhere else in the page, but this one didn't. When I looked at the output, it was not resolving the tilde, so the link was actually pointing at "myserver.net/~/css/main.css", which obviously isn't going to work.

接下来我试着用RESOLVEURL,像这样:

Next I tried using ResolveURL, like so:

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

Visual Studio中甚至不会编译。它甚至不知道是什么意思RESOLVEURL。 (作为一个测试,我坚持同code等几个地方,包括页面标题右边有旁边的链接标记,它工作得很好其他地方!)

Visual Studio wouldn't even compile that. It didn't even know what ResolveURL meant. (As a test, I stuck the same code several other places, including the page title right there next to the link tag, and it worked fine everywhere else!)

我也终于得到它通过提供链接的ID和设置在href在code-后面工作:

I did eventually get it to work by giving the link an ID and setting the href in the code-behind:

--Master page--
<link id="StyleLink" runat="server" rel="stylesheet" type="text/css" />

--Masterpage codebehind file--
StyleLink.Attributes.Add("href", ResolveUrl("~/css/main.css"));

但我在想,为什么我不得不花两个小时与此战斗。为什么摆在首位不是标准〜符号的工作?我GOOGLE了一段时间了,但我无法找到任何关系尤为重要;我能找到最接近的是符号失败的〜讨论时,它是在一个子母版页。

But I'm left wondering why I had to spend two hours fighting with this. Why didn't the standard ~ notation work in the first place? I googled around for a while but I couldn't find anything particularly relevant; the closest I could find was a discussion of ~ notation failing when it was in a sub-master page.

推荐答案

这工作在母版页在我面前现在:

This works in the Master Page in front of me right now:

<head runat="server">
     <link runat="server" href="~/styles/main.css" rel="stylesheet" type="text/css" />
</head>

有关的应用程序的根页面,这转化出到HTML作为这样的:

For a Page in the root of the application, this translates out to the HTML as this:

<link href="styles/main.css" rel="stylesheet" type="text/css" />

有关在根文件夹中的一个页面,这里是什么样子:

For a Page in a folder off the root, here's what it looks like:

<link href="../styles/main.css" rel="stylesheet" type="text/css" />

(这两个页面使用师父,很明显)

(Both pages use that Master, obviously)

替代做法

存储的路径在web配置CSS文件,并修改它在部署。

您甚至可以使用 <一个href=\"https://www.google.com/#sclient=psy-ab&hl=en&source=hp&q=web.config+transformation&pbx=1&oq=web.config+transformation&aq=f&aqi=g4&aql=1&gs_sm=e&gs_upl=6835l6835l2l7003l1l1l0l0l0l0l140l140l0.1l1l0&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=10d598ea5da8a330&biw=1680&bih=900\"相对=nofollow> Web配置转换 的基础上生成类型自动改变它。

Store the path to the CSS file in the web config, and alter it upon deployment.
You can even use Web Config Transformations to change it automatically based on the build type.

这篇关于母版CSS链接无法解析〜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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