简体   繁体   中英

What is the difference between <%: and <%= in ASP.NET MVC?

I can't find this info anywhere. Probably because Google is ignoring the keywords. Is there a difference between using <%: and <%= in your aspx page? They seem interchangeable.

<%: %> is a new thing in ASP.NET MVC 2 . It is the same as <%= Html.Encode("Text") %> . It is recommended to always use <%: %> unless you have some specific reason to not do so (for example, you are rendering data from some file or database that's already been encoded).

The difference is :

<%= "my <text>" %> will output my <text> , which is incorrect HTML

<%: "my <text>" %> will output my &lt;text&gt; , which is better

More details here

@ntcolonel is right on the money. Additionally, for cases where your data has already been encoded, provide it using anything implementing IHtmlString . This prevents double-encoding, and allows you to always use <%: %> .

I believe that ASP.NET 4 shops should gravitate toward enforcing <%: %> by policy.

Also, the new syntax is for ASP.NET 4 in general; not necessarily just MVC, which is great news for WebForms developers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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