Asp.net delimiter <% replaced with <% in head tag?
Asked Answered
P

5

5

Maybe its a stupid question, but i'm having this issue in Visual Studio 2010:

in my Master page i've this code:

<head runat="server">

    <title>App Title</title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
    <link href="<%= App.RootPath %>Css/style.css" rel="stylesheet" type="text/css" />
</head>

for some strange reason the <% is changed at runtime with &lt;%

<%= App.RootPath %> works normal if put anywhere outside head tag.

Anyone has never experienced this and resolved?

UPDATE:

If i put off runat="server" in head tag, it works. But i need it.

Edit:

All of these methods work, but the problem is lack of designer support?

Plastered answered 8/10, 2011 at 8:51 Comment(2)
Check if its somehow related #1875375Yam
I've read that, but it have missed the = sign, so does not apply to this caseCatha
P
2

For now, i've found this workaroud; still searching for the reason of this behaviour.

<link <%= "href=" +App.RootPath +"Css/style.css" %> rel="stylesheet" type="text/css" />
Plastered answered 8/10, 2011 at 9:7 Comment(0)
V
5

The explanation for your trick:

<link <%= "href='" +App.RootPath +"Css/style.css'" %> rel="stylesheet" type="text/css" />

To find the answer generate a compilation exception. Change App.RootPath to App.RootPaths.., then navigate to the source code (it will be shown in the error page). If the compiler matches something like <link href='' rel='' > then it will generate the code to build a corresponding HtmlLink instance. So this is why it parses <%= as a literal string and after that it encodes it.

Your trick cheats the compiler, which is not bad at all.

I believe it does the same thing for meta tags, (HtmlMeta)

Velate answered 8/10, 2011 at 9:19 Comment(0)
P
2

For now, i've found this workaroud; still searching for the reason of this behaviour.

<link <%= "href=" +App.RootPath +"Css/style.css" %> rel="stylesheet" type="text/css" />
Plastered answered 8/10, 2011 at 9:7 Comment(0)
H
2

This should work too.

<link href="<%= App.RootPath + "Css/style.css" %>" rel="stylesheet" type="text/css"/>
Hephaestus answered 8/10, 2011 at 9:27 Comment(0)
R
1

I normally use ResolveUrl:

<link href='<%= Page.ResolveUrl("~Css/style.css") %>' rel="stylesheet" type="text/css"/>
Recognizor answered 8/10, 2011 at 23:25 Comment(0)
C
-1
**problem**
 <link rel="canonical" href="http://www.kayserianadoluhaber.com.tr/haber/<%=kanonikal%>" />

**solved**
 <link rel="canonical" href="http://www.kayserianadoluhaber.com.tr/haber/<%=kanonikal+""%>" />
Carangid answered 15/7, 2018 at 16:26 Comment(1)
please format your answer properly and add more explanation on how it worksJailbreak

© 2022 - 2024 — McMap. All rights reserved.