Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

c# - 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:

 <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.

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.

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 :

@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>
    <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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. 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.

There are two ways to handle it. First is to ClientScriptManager to register a startup script. 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. The other option is to do something like this in your 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. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...