David
David

Reputation: 25450

How to change .ASPX automatic formatting settings (Visual Studio)

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.

Upvotes: 34

Views: 6844

Answers (5)

mike nelson
mike nelson

Reputation: 22136

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

Upvotes: 2

Chad Ruppert
Chad Ruppert

Reputation: 3680

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.

Upvotes: 2

Jeroen
Jeroen

Reputation: 175

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.

Upvotes: 4

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

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.

Upvotes: -4

はると
はると

Reputation: 1499

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.

Upvotes: 2

Related Questions