Friday, October 15, 2010

How to stop automatic newline in ASP.NET code blocks

I want to keep my one liner code blocks in my ASP.NET MVC views as:

  1. <% Html.BeginForm(); %> 

Unfortunately, a not so helpful feature recognize the C# code and reformats to the following:

  1. <%  
  2.    Html.BeginForm(); %> 

Thanks to Erv Walter for solving the problem.

In short, to remove this feature we have to:

  • Go to Tools – Options – Text Editor – HTML – Formatting
  • Select Tag Specific Options…
  • Under Client HTML Tags, add three tags: “%”, “%:”, and “%=”

The %-tag should have line breaks before and after, and no closing tag.

The %: and %= tags should have no closing tag, and no line breaks.

For ReSharper-users, also remember to disable Auto-formatting on semicolon, and on closing brace. Otherwise, ReSharper will interfere and reformat.

No comments: