Search code examples
asp.net-mvcasp.net-mvc-3webforms-view-engine

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


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 one 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


Solution

  • 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.