ResolveUrl Problem in Master Page

随声附和 提交于 2019-11-29 08:42:11

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.

Matt

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">?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!