开发者

ASP.net MVC 3 can i replace all <%= in my views with <%:?

开发者 https://www.devze.com 2023-04-01 17:51 出处:网络
i have an MVC 3 site using asp.net 4 in my views i have used <%= for outputting data. reading this post by phil haack -> http://haacked.com/archive/2009/09/25/html-encoding-code-nuggets.aspx

i have an MVC 3 site using asp.net 4

in my views i have used <%= for outputting data.

reading this post by phil haack -> http://haacked.com/archive/2009/09/25/html-encoding-code-nuggets.aspx

He suggests that <%= should be a distant memory, as long as your using .net 4.

i have done a find and replace all, on on开发者_高级运维e of my views replacing

<%= to <%:

and everything works as normal.

so my question, can i go ahead and do a global replace on all my views? are there any issues to be aware of ?

Truegilly


Be careful though as if you have something like this:

<%= Html.Encode(Model.Foo) %>

you might get double encoding when you end up with:

<%: Html.Encode(Model.Foo) %>

Whereas the correct would be:

<%: Model.Foo %>

Personally I tend to always use:

<%= Html.DisplayFor(x => x.Foo) %>

So, it's not as easy as a simple search & replace. There might be some breaking modifications.


Everything that Darin said and this:

After-format with HTML

Be aware when you after format your output and add some HTML inside it as in

<%= this.Model.Explanation.Replace("\n", "<br/>") %>

This clearly won't work after you'd simply replace <%= with <%:.

0

精彩评论

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

关注公众号