开发者

Asp.net delimiter <% replaced with &lt;% in head tag?

开发者 https://www.devze.com 2023-04-11 20:03 出处:网络
Maybe its a stupid question, but i\'m having this issue in Visual Studio 2010: in my Master page i\'ve this code:

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 n开发者_运维知识库ormal 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?


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)


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


This should work too.

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


I normally use ResolveUrl:

<link href='<%= Page.ResolveUrl("~Css/style.css") %>' rel="stylesheet" type="text/css"/>


**problem**
 <link rel="canonical" href="http://www.kayserianadoluhaber.com.tr/haber/<%=kanonikal%>" />

**solved**
 <link rel="canonical" href="http://www.kayserianadoluhaber.com.tr/haber/<%=kanonikal+""%>" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号