Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 772
David Avatar asked Mar 11 '09 12:03

David


People also ask

How do I set Auto format in Visual Studio?

Auto formatting settings in Visual Studio Show activity on this post. Select the text you want to automatically indent. Click menu Edit → Advanced → *Format Selection, or press Ctrl + K , Ctrl + F . Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.

How do I change the code format in Visual Studio?

The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.


1 Answers

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.

like image 144
Jeroen Avatar answered Sep 18 '22 12:09

Jeroen