Html 修复 Visual Studio 中 aspx 文件的格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2978049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fix formatting of aspx file in Visual Studio?
提问by Vaccano
I have a file with a lot of this kind of thing in it:
我有一个包含很多此类内容的文件:
<asp:TableCell ID="TableCell9" runat="server">Company
Organization:</asp:TableCell><asp:TableCell ID="TableCell10" runat="server">
How can I get the formatter to change it to shows like this:
如何让格式化程序将其更改为如下所示:
<asp:TableCell ID="TableCell9" runat="server">Company Organization:</asp:TableCell>
<asp:TableCell ID="TableCell10" runat="server">
What I have tried:
我尝试过的:
I opened Tools->Options then browsed to Text Editor. I got a list of Languages. I selected HTML because that is the language that controls formatting for aspx files.
我打开工具-> 选项,然后浏览到文本编辑器。我得到了一个语言列表。我选择 HTML 是因为它是控制 aspx 文件格式的语言。
I changed "tag wrapping" to not have "Wrap tags when exceeding specified length" checked.
我将“标签包装”更改为没有选中“超过指定长度时包装标签”。
I then pressed ctrl+k, ctrl+d (Format the document). This did not fix the problem.
然后我按 ctrl+k、ctrl+d(格式化文档)。这并没有解决问题。
What I don't want to have to do:
我不想做的事情:
Edit the file manually to fix all the tags.
手动编辑文件以修复所有标签。
Any ideas?
有任何想法吗?
回答by TheLukeMcCarthy
I was having the same problem, and I found the answer on Scott Gu's blog.
我遇到了同样的问题,我在 Scott Gu 的博客上找到了答案。
The solution is to setup the formatting rules in Visual Studio (right click on any tag, click on Formatting and Validation, click on Tag Specific Options)
解决方案是在 Visual Studio 中设置格式规则(右键单击任何标签,单击格式和验证,单击标签特定选项)
When press ctrl-k, ctrl-d the document will format as per your settings. If a closing tag does not move to a new line, it's because there is no space between the end tag and what is preceding it. This happens because Visual Studio is being careful to not change how the page is rendered.
当按 ctrl-k、ctrl-d 时,文档将根据您的设置进行格式化。如果结束标签没有移到新行,那是因为结束标签和它前面的标签之间没有空格。发生这种情况是因为 Visual Studio 小心翼翼地不更改页面的呈现方式。
For the full blog post checkout Scott Gu's blog post.
有关完整的博客文章,请查看 Scott Gu 的博客文章。
回答by Vaccano
I found a way that works.
我找到了一种有效的方法。
In the list with HTML there is a File Extension Option.
在带有 HTML 的列表中有一个文件扩展名选项。
I added ASPX to that list and said to edit it with an XML editor (I suppose I could have just right clicked it and opened it with the xml editor, but I did not think of that till after).
我将 ASPX 添加到该列表中,并说用 XML 编辑器编辑它(我想我可以右键单击它并用 xml 编辑器打开它,但直到后来我才想到这一点)。
Anyway, once it is open as an XML File I formatted it (ctrl+k, ctrl+d) and it formated the way I wanted it. I then removed the extension mapping and reopend the file again.
无论如何,一旦它作为 XML 文件打开,我就对其进行了格式化(ctrl+k、ctrl+d)并按照我想要的方式对其进行格式化。然后我删除了扩展映射并再次重新打开文件。
One last formatting adjusted the tab spacing and I was perfect!
最后一种格式调整了制表符间距,我很完美!
Note:When I did this the top line had this change made: from Page Language="C#"
to Page="" Language="C#"
I changed it back manually and all seemed to be well.
注意:当我这样做时,顶行进行了此更改:从Page Language="C#"
到Page="" Language="C#"
我手动将其改回,一切似乎都很好。
Later Note:Sadly, Visual Studio messes up the formatting every time I run the solution. Note that pressing ctrl+k, ctrl+d is fine and keeps my nice formatting, but when I run, Visual Studio collapses it down to a very unreadable format. :( Don't know how to fix it....
稍后注意:遗憾的是,每次我运行解决方案时,Visual Studio 都会弄乱格式。请注意,按 ctrl+k、ctrl+d 很好并保持我的漂亮格式,但是当我运行时,Visual Studio 将其折叠为非常不可读的格式。:( 不知道怎么解决....
回答by krolth
The answer should be: Tools-Options->Text Editor->HTML-Format->Tag Specific Options Then in ASP.NET Controls look for your tag. If it's not there you might need to Add it (New Tag) and set the Line breaks as you want them.
答案应该是:工具-选项->文本编辑器->HTML-格式->标签特定选项然后在 ASP.NET 控件中查找您的标签。如果它不存在,您可能需要添加它(新标签)并根据需要设置换行符。
However I've found that this does not always work.Visual Studio respects the per tag colorization but Line breaks are not always respected.
但是我发现这并不总是有效。Visual Studio 尊重每个标签的着色,但并不总是尊重换行符。
So if this doesn't work for you, you might need to do a Search-Replace hack (Look for and replace with a NewLine in the middle), thenk try Format document and that should work.
因此,如果这对您不起作用,您可能需要执行 Search-Replace hack(查找并替换中间的 NewLine),然后尝试格式化文档,这应该可以工作。