简体   繁体   English

母版页中的ResolveUrl问题

[英]ResolveUrl Problem in Master Page

Okay, 好的,

I know it is weird but when I put this code between <head runat="server"></head> in master.page, this is how it renders into: 我知道这很奇怪,但是当我将此代码放在master.page中的<head runat="server"></head>时,它就是这样呈现的:

 <link id="ctl00_Link1" rel="shortcut icon" href="../%3C%25%20ResolveUrl(%22~/Resources/Pictures/Shared/Misc/favicon.ico%22);%20%25%3E" type="image/x-icon" />

It doesn't see something asp.net needs to take care of. 它没有看到asp.net需要处理的事情。

This is the original code : 这是原始代码:

<link id="Link1" rel="shortcut icon" href='<%=ResolveUrl("~/Resources/Pictures/Shared/Misc/favicon.ico") %>' type="image/x-icon" runat="server" />

Basically Asp.Net doesn't take care of the code below and renders as a normal html. 基本上,Asp.Net不会处理下面的代码,而是呈现为普通的html。

How can I get over this problem? 我怎么能克服这个问题?

Thanks in advance... 提前致谢...

Edit and Resolved 编辑并解决

Okay people, there is no way for doing this. 好的,没有办法这样做。 I've finally figured out because ResolveUrl or ResolveClientUrl is only working for these below : 我终于弄清楚了,因为ResolveUrl或ResolveClientUrl仅适用于以下这些:

@import '<%= ResolveUrl("~/Resources/Styles/MainMaster/MainDesign.css") %>';
<script src='Resources/Scripts/Libraries/jquery-1.4.2.js' type="text/javascript"</script>

it is too literal for link so you need to put link elements in body tag like : 对于链接而言,它太文字化了,因此您需要将链接元素放在body标签中,例如:

<body>
    <link id="iconOne" rel="shortcut icon" type="image/x-icon" href="Resources/Pictures/Shared/Misc/favicon.ico"/>
    <link id="iconTwo" rel="icon" href='Resources/Pictures/Shared/Misc/favicon.ico' type="image/ico" />
</body>

So, the reason you ran into your first issue was because the link tag had runat="server" This tells asp.net to treat it as a server control, rather then a literal. 因此,您遇到第一个问题的原因是因为链接标记具有runat="server"这告诉asp.net将其视为服务器控件,而不是文字。 Because its a server control, your scriptlet tag ( <%= %> ) isn't really doing anything, since its a server control property it is treating it as literal text. 因为它是服务器控件,所以scriptlet标记( <%= %> )实际上并没有做任何事情,因为它是服务器控件属性,因此将其视为文本。

There are two ways to handle it. 有两种处理方法。 First is to ClientScriptManager to register a startup script. 首先是向ClientScriptManager注册启动脚本。 This will put your link tag inside the body, which is the way microsoft says you should do it, but aesthetically isn't that nice. 这会将您的链接标记放入体内,这是Microsoft所说的方式,但从美学上讲并不是很好。 The other option is to do something like this in your Page_Load 另一种选择是在您的Page_Load中执行类似的操作

var link = new HtmlGenericControl("link");
link.Attributes.Add("rel", "shortcut icon");
link.Attributes.Add("src", ResolveUrl("~/Resources/Pictures/Shared/Misc/favicon.ico"));
link.Attributes.Add("type", "image/x-icon");

Header.Controls.Add(link);

This builds out a control programatically, then adds it to the controls collection on the head, which will render as what you want at the end of the head tag. 这将以编程方式构建一个控件,然后将其添加到头部的控件集合中,该控件将在head标签的结尾呈现为所需的内容。 Problem with this is that its a bit more work, and its better to avoid monkeying with control collections at the code behind level if you can get away with it. 问题在于这需要更多的工作,并且最好避免在后面的代码中胡乱使用控件集合。

That might be making it a little more complicated than it needs to be. 这可能会使它变得比需要的复杂。 Have you tried simply using ~ in icon path, and setting <head runat="server"> ? 您是否尝试过在图标路径中仅使用~并设置<head runat="server">

For example: 例如:

<head runat="server">
    ...
    <link rel="icon" href="~/Resources/Pictures/Shared/Misc/favicon.ico" 
        type="image/x-icon" />
    <link rel="shortcut icon" href="~/Resources/Pictures/Shared/Misc/favicon.ico"
        type="image/x-icon" />
    ...
</head>

Related SO answer : Favicon Not Showing 相关SO答案 :网站图标未显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM