views:

1253

answers:

5

When typing code in a .aspx file (an MVC view in this case), Visual Studio applies two types of formatting, one to the regular html tag structure (which can be controlled from Tools->Options->Text Editors->Html) and another to content inside the <% %> tags.

I've run into two annoyances with the second type of automatic formatting, the <% %> content formatting.

First is how new lines are added to statement blocks:

If I type this:

<% if(condition) { %>
    ...
<% } %>

It is autocorrected to this:

<% if(condition) 
   { %>
    ...
<% } %>

While the correction is right if this was a .cs file, for the tag soup that is .aspx files I find the first far more readable. Is there a way to turn off this behavoir without affecting the formatting of .cs files?

Second, whenever I write something like this:

<%=Html.ActionLink("Report","ListItems") %>

I can't find a way to make it automatically format into this (add spacing where appropriate):

<%=Html.ActionLink("Report", "ListItems") %>

In code you normally need a ; or } to signal that you are done a line/section of code so that Visual Studio can format it. Is there a way to give this signal for a <%= type of expression?

CLARIFICATION

This question is about the formatting of code inside <% %> tags without affecting the formatting of regular C# source files. It says right in the first paragraph that I am completely aware of the Text Editors menu.

+1  A: 

You're able to change the autoformatting in this menu:
Tools -> Options -> Text Editor

You can for example change the if statement's new line, under:
C#
- Formatting
-- New Lines
--- Place open brace on new line for control blocks

Hope this helps.

Eibx
That would seem to affect the formatting of all .cs files, which is what we want to avoid. We only want to change how the tag soup code gets formatted.
David
A: 

If you have never customized your environment, I suggest spending some time in the Tools >> Options dialog. There are a lot of neat things you can do to customize your environment, as well as alter the default way Visual Studio acts.

I have not tried the new lines on an MVC view, so I am not completely sold it works, but it would be where I would try first, as well. It does work in a C# file, code behind or otherwise.

Gregory A Beamer
+2  A: 

The Text Editors menu is all there is, and it doesn't allow you to do the things you want. When you right-click on an aspx page, click "Formatting and Validation..." and then click the button "Tag Specific Options..." you get a bunch of settings per client- or server-tag, but not for the <%-tags.

As for the second part of your question, Ctrl-E,D formats your document, but does not insert the spacing you want.

Jeroen
+1  A: 

Currently the only answer I am aware of is to write macros using regexes and assign them to easy to use chords. I have previously answered a question where the autoformatting would change closing braces to:

<%
     }
     %>

You can find that answer, including the code for the macro used here.

Chad Ruppert
+1  A: 

According to microsoft this is a bug in visual studio that has just been fixed - although I have not yet seen the fix so cannot confirm that they really have. It seems unlikely after 10 years they would have suddenly fixed it, but we'll see. See http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=514691

mike nelson