asp.net-mvc 为什么 Visual Studio 代码格式不能正确用于 Razor 标记?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6902204/
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
Why doesn't Visual Studio code formatting work properly for Razor markup?
提问by ProfK
Or, should I rather ask, when will VS code formatting work properly for Razor markup? The formatting works for most structures, but it seems to choke on 'if' blocks. The code below is as it is formatted by VS. It is very easy to fix this case, with one more indent, but I nicely accepted the formatting in everyday use, and like to use it often for the bulk of my code, so I'd rather avoid manual formatting if possible. Right now I just leave it as VS formats it.
或者,我应该问,什么时候 VS 代码格式可以为 Razor 标记正常工作?格式化适用于大多数结构,但它似乎在 'if' 块上窒息。下面的代码是由 VS 格式化的。修复这种情况很容易,多缩进一个,但我很好地接受了日常使用中的格式,并且喜欢经常在我的大部分代码中使用它,所以如果可能的话,我宁愿避免手动格式化。现在我只是将它保留为 VS 格式。
@{
if (User.Identity.IsAuthenticated)
{
<text>Hello </text>
@Html.Display("@ViewBag.UserName") <text> - </text>
@Html.ActionLink("Sign Out", "LogOff", "Account", null, new { style = "font-weight: bold;" })
}
}
I think it's important for readability that, e.g. in the above, the body of the if block is indented, besides just looking nicer.
我认为对于可读性很重要,例如在上面,if 块的主体缩进,除了看起来更好。
采纳答案by Charles Burns
Be sure to set the editor to use space characters and not tabs. The editor seems to completely lose its mind when tabs are used. This is a shame because all those space characters end up in the actual HTML output, greatly increasing the data transfer size. What I do is manually supplement the automatic formatting as I type. Not ideal, but hopefully Microsoft will have this figured out for the next service pack.
确保将编辑器设置为使用空格字符而不是制表符。使用选项卡时,编辑器似乎完全失去了理智。这是一种耻辱,因为所有这些空格字符最终都会出现在实际的 HTML 输出中,从而大大增加了数据传输的大小。我所做的是在键入时手动补充自动格式。不理想,但希望微软能在下一个服务包中解决这个问题。
回答by Josh Mouch
I found one "solution" that allows you to continue using tab indentation and have correct formatting. It's more of a pattern. The key is to use razor code blocks instead of inline code.
我找到了一个“解决方案”,它允许您继续使用制表符缩进并具有正确的格式。它更像是一种模式。关键是使用 razor 代码块而不是内联代码。
So for example, replace the following:
例如,替换以下内容:
<div>
<div>
@if (true)
{
<b>Hi</b>
}
</div>
</div>
with:
和:
<div>
<div>
@{
if (true)
{
<b>Hi</b>
}
}
</div>
</div>
The latter will format correctly, but the former won't.
后者会正确格式化,但前者不会。
Keep in mind, the formatting isn't perfect, but it's better than before.
请记住,格式并不完美,但比以前更好。
回答by marcind
It does not work correctly in all cases because it's a difficult problem to solve. Essentially you have 3 different editors (HTML, C#, and Razor) all interacting over the same text buffer. There are some cases (like this one) where the interactions have bugs. But we are working on improving the editor for the next release of Razor.
它并非在所有情况下都能正常工作,因为这是一个难以解决的问题。本质上,您有 3 个不同的编辑器(HTML、C# 和 Razor),它们都在同一个文本缓冲区上进行交互。在某些情况下(例如这个),交互存在错误。但是我们正在努力改进下一个 Razor 版本的编辑器。
回答by Derek Kalweit
A better alternative here(rather than using spaces for tabs), is to change the block indenting for HTML and C#/VB to "Block" instead of "Smart". This isn't a full solution, but IMO is a far less painful work-around than using spaces!
这里更好的替代方法(而不是使用空格作为制表符)是将 HTML 和 C#/VB 的块缩进更改为“块”而不是“智能”。这不是一个完整的解决方案,但与使用空格相比,IMO 的痛苦程度要小得多!
回答by Sidron
I found another solution for this. Just select all code in file, click Shift + tab to remove all tabs before code, copy and paste it. Visual studio automatically format code. Work on VS 2013 .cshtml file
我为此找到了另一个解决方案。只需选择文件中的所有代码,单击 Shift + tab 删除代码前的所有选项卡,复制并粘贴它。Visual Studio 自动格式化代码。处理 VS 2013 .cshtml 文件
回答by Danny Law
In my case it was resharper overriding formatting options.
在我的情况下,它是 resharper 覆盖格式选项。
If your using reshaper and getting this issue try this...
如果您使用整形器并遇到此问题,请尝试此...
Resharper >> Options >> Razor >> Editor & Formatting >> Untick “Auto-format on enter”
Resharper >> 选项 >> Razor >> 编辑器和格式 >> 取消勾选“输入时自动格式化”
回答by Craig Poole
I know it's not really the answer you're looking for but I've used WriteLiteral to get around my formatting issues.
我知道这不是您真正要寻找的答案,但我已经使用 WriteLiteral 来解决我的格式问题。
For example, when I write:
例如,当我写:
<div>
@foreach (var item in Model) {
if (condition) {
@:</div><div>
}
<a href="@item.Url">@item.Label</a>
}
</div>
Visual Studio tries to change it to:
Visual Studio 尝试将其更改为:
<div>
@foreach (var item in Model) {
if (condition) {
@:
</div><div>
}
<a href="@item.Url">@item.Label</a>
}
</div>
Which causes the page to throw an error.
这会导致页面抛出错误。
If you use WriteLiteral you can fool the formatter into ignoring the line but it ain't pretty:
如果您使用 WriteLiteral,您可以欺骗格式化程序忽略该行,但它并不漂亮:
<div>
@foreach (var item in Model) {
if (condition) {
WriteLiteral("</div><div>");
}
<a href="@item.Url">@item.Label</a>
}
</div>
回答by Mikayil Abdullayev
Right now I'm on VS2013 ASP.NET MVC 5 and I still have that problem. What I found to be a lot helpful is to put the first expression on the same line where the opening block symbol is (@{). That way razor code formatting produces a far better result. Here are the before and after cases:
现在我在 VS2013 ASP.NET MVC 5 上,我仍然有这个问题。我发现将第一个表达式放在开头块符号是 ( @{)的同一行上很有帮助。这样剃刀代码格式会产生更好的结果。以下是之前和之后的案例:
BEFORE
前
AFTER
后
回答by FredyWenger
I work with VS2017 15.9.2 and still have the problem.
After change the editor settings to use spaces instead of tabs, the behavior in editing (e.g. copy - paste code lines) is way better, but "Format Document" still add wrong indents by every call.
我使用 VS2017 15.9.2 并且仍然有问题。
将编辑器设置更改为使用空格而不是制表符后,编辑行为(例如复制 - 粘贴代码行)会更好,但“格式化文档”仍然会在每次调用时添加错误的缩进。
No solution, but a short update:
没有解决方案,但有一个简短的更新:
It seems as the issue is solved partial in Visual Studio 2019 version 16.0 Preview 2.1
Link to MS for the issue
问题似乎在 Visual Studio 2019 版本 16.0 Preview 2.1
Link to MS for the issue 中部分解决
Further short update:
I have found a (bad and ugly) workaround (to write the whole code to a razor control in ONE line.
You can find the details here Workaround to wrong indentation Razor Controls
进一步的简短更新:
我发现了一个(糟糕且丑陋的)解决方法(将整个代码写入一行中的剃刀控件。您可以在此处找到详细信息错误缩进 Razor 控件的解决方法
回答by guest
I recommend you prevent automatic formatting to trigger by commenting the piece of code where you paste. This way things don't get broken on paste.
我建议您通过注释粘贴的代码段来防止触发自动格式化。这样东西就不会被糊弄坏了。


