简体   繁体   中英

What's the difference between <% and <%:

I couldn't find anything through Google, I suppose the search terms are too much to handle. The VS built-in help only explains <%, which is just embedded code. There is no mention of <%:

EDIT: My book says, that the only difference is that <%: returns a MvcHtmlString. Why do we need an extra symbol for <%: just to return something?

EDIT2: An example:

<% Html.RenderAction("xxx", "xxx"); %>

Nothing is returned, that's why we have to say <%, because it would htmlencode? But what if I render something in .RenderAction() that needs to be htmlencoded too? Or does that not make sense?

Thanks for any help! It would really help me to understand asp.net.

Using <%: you are already tell the page to HtmlEncode what is inside the <%: %> tags. and it was presented in .Net 4.

EDIT: Yes, MvcHtmlString is already HtmlEncoded.

PS: If you want (sometimes) not to encode text as HTML stick with the old ones but you have to know that if that data was provided by users it is considered a security threat which could be used as an exploit to impose XSS attacks against your web site/application.

两者都用于在视图上呈现文本,但是当您使用<%:时,文本将自动进行HTMLEncoded。

As indicated in other answers, it is for Html encoding. Here's a blog entry explaining .

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