如何使代码缩进在 vbhtml razor 文件中正确运行?

发布于 2024-12-18 04:27:53 字数 1006 浏览 3 评论 0原文

这让我陷入困境。我是一名长期 VB.NET 表单开发人员,对 ASP.NET 非常陌生,对 MVC 完全陌生。我正在创建使用 VB.NET Razor 语法的 vbhtml 页面,并且我似乎一直在与试图我的代码缩进不正确。以基于新 Razor 视图的模板页面为例:

@Code
    Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div>
        @If True Then
            @<ul>
                @For x = 1 To 2

                Next
            </ul>
                End If '<-- Randomly indented too far
    </div>
</body>
</html>

在上面的示例中,一旦我在 Next 之后按回车键,End If< /code> 下面两行从应有的位置随机向前跳转两个选项卡。在其他示例中,我遇到了一个圆圈,其中将一条线推到正确的位置会使另一条线脱离位置,反之亦然。

我现在很生气,我很乐意完全禁用自动缩进并自己管理它,但我什至不知道如何做到这一点!根据另一个线程的建议,我禁用了 HTML 页面的缩进,但停止的只是 HTML 标记的缩进 - 代码块仍然到处滑动。

我认为扩展可能会导致问题,但我将它们全部禁用并重新启动,问题仍然存在。我是否在做一些根本性错误的事情?我很难相信微软会发布如此糟糕的东西,所以看起来更有可能是我没有正确使用它。

This is driving me round the bend. I'm a long time VB.NET forms developer, quite new to ASP.NET and completely new to MVC. I'm creating vbhtml pages that use the VB.NET Razor syntax, and I seem to be constantly fighting against the UI which is trying to indent my code incorrectly. Take the following example, based on the template page for a new Razor view:

@Code
    Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div>
        @If True Then
            @<ul>
                @For x = 1 To 2

                Next
            </ul>
                End If '<-- Randomly indented too far
    </div>
</body>
</html>

In the above example, as soon as I hit return after Next, End If two lines below randomly jumps two tabs forward from where it should be. In other examples I've hit a circle where pushing one line to the correct place throws another line out of position and vice versa.

I'm so annoyed at this point I'd be happy to disable auto-indentation completely and just manage it myself, but I can't even find out how to do that! Following advice on another thread I disabled indentation for HTML pages but all that stops is indentation of HTML tags - the code blocks still slide around all over the place.

I thought an extension might be causing the problem but I disabled them all and restarted and the problem remains. Am I doing something fundamentally wrong? I find it hard to believe Microsoft would release something so poor so it seems more likely I'm just not using it right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆で 2024-12-25 04:27:53

我在另一个问题上找到了一个(时尚的)解决方案(在发布这个问题之前我确实进行了努力搜索,但找不到任何东西): 为什么 Visual Studio 代码格式不能正常用于 Razor 标记?

本质上,解决方案似乎是确保您的代码使用空格而不是制表符作为空白。虽然由于空白增加而增加了页面的整体大小,但它确实减轻了问题(虽然没有完全消除它)。在链接的线程中,似乎与 Microsoft 有联系的人承认这确实是与 HTML 和 VB.NET 重叠格式化程序相关的错误,他们希望在新版本中对其进行改进。我已将每个缩进减少到 2 个空格,以减少带宽影响。

感谢那些做出贡献的人。

I found a solution (of a fashion) on another question (I really did search hard before posting this question and couldn't find anything): Why doesn't Visual Studio code formatting work properly for Razor markup?

Essentially the solution seems to be to ensure that your code uses spaces instead of tabs for the whitespace. Whilst increasing the overall size of the page because of increased whitespace, it does lessen the problem (whilst not eliminating it completely). On the linked thread, someone who appears to be connected with Microsoft has acknowledged it is indeed a bug related to the overlapping formatters for HTML and VB.NET which they hope to improve in a new release. I've dropped to 2 spaces per indent to lessen the bandwidth impact.

Thanks to the guys who contributed.

绅士风度i 2024-12-25 04:27:53

这里更好的替代方案(而不是使用制表符的空格)是将 HTML 和 C#/VB 的块缩进更改为“块”而不是“智能”。这不是一个完整的解决方案,但在我看来,这是一个比使用空格更轻松的解决方法!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文