如何更改.ASPX自动格式设置(Visual Studio)

时间:2022-11-21 02:47:53

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.

在.aspx文件(本例中是MVC视图)中输入代码时,Visual Studio应用两种格式,一种用于常规的html标记结构(可以从工具->选项->文本编辑器-> html控制),另一种用于<% >标记中的内容。

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?

如果这是.cs文件,那么更正是正确的,对于.aspx文件的标签汤,我发现第一个要可读性得多。是否有一种方法可以在不影响.cs文件格式的情况下关闭这种行为?

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?

在代码中,通常需要a;或}表示您已经完成了代码的行/段,以便Visual Studio可以对其进行格式化。是否有方法为<%=类型的表达式赋予此信号?

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.

这个问题是关于在不影响常规c#源文件格式的情况下,在<% >标签内对代码进行格式化的问题。它在第一段说我完全了解文本编辑器菜单。

5 个解决方案

#1


4  

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.

文本编辑器菜单就在那里,它不允许你做你想做的事情。当您右键单击一个aspx页面时,单击“格式化和验证……”,然后单击“标记特定选项……”按钮,您将获得每个客户机或服务器标记的一系列设置,但不是<%-标记的设置。

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

至于您的问题的第二部分,Ctrl-E,D格式化您的文档,但不插入您想要的间隔。

#2


2  

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

您可以更改此菜单中的自动格式:Tools ->选项->文本编辑器

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

例如,您可以更改if语句的新行,在:c# - Formatting——new Lines——在控制块的新行上放置一个展开括号

Hope this helps.

希望这个有帮助。

#3


2  

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:

目前,我知道的唯一答案是使用regexes编写宏,并将它们分配给易于使用的和弦。我之前回答过一个问题,自动格式化将关闭括号更改为:

<%
     }
     %>

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

您可以找到这个答案,包括这里使用的宏的代码。

#4


2  

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

根据微软的说法,这是一个在visual studio中已经修复的bug——尽管我还没有看到修复程序,所以无法确认它们是否真的存在。十年后,他们似乎不太可能突然修复它,但我们将拭目以待。参见http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=514691

#5


-4  

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.

如果您从来没有自定义您的环境,我建议您花一些时间在工具>>选项对话框。您可以做许多简单的事情来定制您的环境,以及更改Visual Studio的默认行为方式。

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.

我还没有尝试过MVC视图上的新行,所以我并没有完全相信它是有效的,但是它也是我首先尝试的地方。它可以在c#文件、代码或其他文件中工作。

#1


4  

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.

文本编辑器菜单就在那里,它不允许你做你想做的事情。当您右键单击一个aspx页面时,单击“格式化和验证……”,然后单击“标记特定选项……”按钮,您将获得每个客户机或服务器标记的一系列设置,但不是<%-标记的设置。

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

至于您的问题的第二部分,Ctrl-E,D格式化您的文档,但不插入您想要的间隔。

#2


2  

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

您可以更改此菜单中的自动格式:Tools ->选项->文本编辑器

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

例如,您可以更改if语句的新行,在:c# - Formatting——new Lines——在控制块的新行上放置一个展开括号

Hope this helps.

希望这个有帮助。

#3


2  

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:

目前,我知道的唯一答案是使用regexes编写宏,并将它们分配给易于使用的和弦。我之前回答过一个问题,自动格式化将关闭括号更改为:

<%
     }
     %>

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

您可以找到这个答案,包括这里使用的宏的代码。

#4


2  

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

根据微软的说法,这是一个在visual studio中已经修复的bug——尽管我还没有看到修复程序,所以无法确认它们是否真的存在。十年后,他们似乎不太可能突然修复它,但我们将拭目以待。参见http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=514691

#5


-4  

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.

如果您从来没有自定义您的环境,我建议您花一些时间在工具>>选项对话框。您可以做许多简单的事情来定制您的环境,以及更改Visual Studio的默认行为方式。

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.

我还没有尝试过MVC视图上的新行,所以我并没有完全相信它是有效的,但是它也是我首先尝试的地方。它可以在c#文件、代码或其他文件中工作。