Razor Helper 语法自动格式化丑陋。如何修复?

发布于 2024-11-16 16:15:33 字数 804 浏览 3 评论 0原文

所以我只是对 Visual Studio 格式化 razor 代码的方式感到不满。我一直对 Visual Studio 以及它如何格式化 UI 代码有一些问题,它似乎总是做得非常糟糕,业界不想效仿。

所以这个例子看起来真的很愚蠢。我正在尝试找出是否有模组或方法可以解决这个问题。它看起来真的很糟糕。

有人知道这件事吗?哈哈

@using Company.Mobile2.Enums
@helper BidsByShipment(string generatedId, int bidsCount, int activeBidsCount)
    {
        if (bidsCount > 0)
        {
    <a class="Company-listview-link Company-listview-bids" href="/Shipping/Bids/ByShipment?id={0}">
        @if (activeBidsCount > 0)
        {
            <text>@bidsCount (@activeBidsCount @GetStr("Company"))</text>
        }
        else
        {
            <text>@bidsCount</text>
        }
    </a>
        }
        else
        {
    <text>0 @GetStr("Company")</text>
        }
}

So I just have a beef with the way Visual Studio formats razor code. I've always had some problems with visual studio and how it formats UI code, it always seems to do a real super bad job that the industry doesn't want to follow.

So the example looks real real stupid. And I'm trying to figure out if there are mods or ways to fix this issue. It just looks real real bad.

Anyone know anything about this? lol

@using Company.Mobile2.Enums
@helper BidsByShipment(string generatedId, int bidsCount, int activeBidsCount)
    {
        if (bidsCount > 0)
        {
    <a class="Company-listview-link Company-listview-bids" href="/Shipping/Bids/ByShipment?id={0}">
        @if (activeBidsCount > 0)
        {
            <text>@bidsCount (@activeBidsCount @GetStr("Company"))</text>
        }
        else
        {
            <text>@bidsCount</text>
        }
    </a>
        }
        else
        {
    <text>0 @GetStr("Company")</text>
        }
}

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

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

发布评论

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

评论(5

铁憨憨 2024-11-23 16:15:33

显然目前没有办法解决这个问题,这是他们在另一个相关问题中回答的内容: 为什么 Visual Studio 代码格式不能正常用于 Razor 标记?

Apparently there's no way around it for the moment, this is what they have answered in another related question: Why doesn't Visual Studio code formatting work properly for Razor markup?

月隐月明月朦胧 2024-11-23 16:15:33

您是否已将 Visual Studio 设置为使用制表符缩进?这揭示了 Razor 格式化错误,它插入空格而不是应有的制表符。解决方法是切换到空格缩进。

Do you have Visual Studio set up to use tab indentation? This reveals a Razor formatting bug where it inserts spaces instead of tabs as it should. The workaround is to switch to space indentation.

牵强ㄟ 2024-11-23 16:15:33

C# 代码的格式与 HTML 代码分开。如果你想要正确的缩进,那么只需在你期望缩进的地方放置一些无用的包装标签,你就会得到缩进。但这将是一种反模式。

这是代码。对于像您定义的函数,我不确定它是否真的有效。

@using Company.Mobile2.Enums
<div>

@helper BidsByShipment(string generatedId, int bidsCount, int activeBidsCount)
    {
        if (bidsCount > 0)
        {
         <a class="Company-listview-link Company-listview-bids" href="/Shipping/Bids/ByShipment?id={0}">
        @if (activeBidsCount > 0)
        {
            <text>@bidsCount (@activeBidsCount @GetStr("Company"))</text>
        }
        else
        {
            <text>@bidsCount</text>
        }
        </a>
        }
        else
        {
         <text>0 @GetStr("Company")</text>
        }
}
<div>

The C# code formats separately from the HTML code. If you want the proper indentation then just put some useless wrapper tags wherever you expect there to be indentation and you'll get the indentation. This would be an anti-pattern though.

Here is code. For a function like you've defined I"m not sure if that actually works.

@using Company.Mobile2.Enums
<div>

@helper BidsByShipment(string generatedId, int bidsCount, int activeBidsCount)
    {
        if (bidsCount > 0)
        {
         <a class="Company-listview-link Company-listview-bids" href="/Shipping/Bids/ByShipment?id={0}">
        @if (activeBidsCount > 0)
        {
            <text>@bidsCount (@activeBidsCount @GetStr("Company"))</text>
        }
        else
        {
            <text>@bidsCount</text>
        }
        </a>
        }
        else
        {
         <text>0 @GetStr("Company")</text>
        }
}
<div>
几度春秋 2024-11-23 16:15:33

我制作了一个用于格式化 razor 文档的扩展。

安装:

1) 在扩展中搜索“razor-formatter”

2) 按 CTRL + P,然后输入下面的命令并按 Enter:

ext install Kookweb.razor-formatter

VSCode 市场上的链接:

https://marketplace.visualstudio.com/items?itemName=Kookweb.razor-formatter

github 上的源代码:

https://github.com/Kookweb-ir/razor-formatter

当然这不是最好的格式化程序,但这只是目前的格式化程序。

这只是一个简单的 HTML 美化器,适用于 razor 文档。如果有人致力于它并使其完美,我会很高兴。

i make an extension for formatting razor document.

Install:

1) in extensions search for "razor-formatter"

2) press CTRL + P AND then enter command below and press enter:

ext install Kookweb.razor-formatter

Link on VSCode marketplace:

https://marketplace.visualstudio.com/items?itemName=Kookweb.razor-formatter

source on github:

https://github.com/Kookweb-ir/razor-formatter

Of course this isn't best formatter, but this is only formatter for now.

this is just a simple HTML beautifier that works on razor documents. i will be happy if anybody works on it and make it perfect.

z祗昰~ 2024-11-23 16:15:33

对于所有抱怨 Visual Studio 的人来说,我认为令人印象深刻的是,它允许您在 HTML 和 C# 之间切换,而无需被告知您正在使用哪种语言。

从更实际的角度来看,我认为我的建议是将上面显示的许多内容结合起来。具体来说...

  1. 避免使用 @: 来表示 HTML 的文字字符串。当您重新格式化代码时,Visual Studio 经常会在其后添加一行,即使不这样做,您也可能会陷入无限递归,使用 @ 然后切换回代码,等等。对于未在 HTML 标记中编码的内容,请使用 WriteLiteral,如上所述;否则,当您使用 .如果……
  2. 您使用了在 @{ ... } 块中插入代码的奇妙想法。

鉴于这两个,我发现 CTRL K、D 重新格式化代码为表块提供了完美的结果,这让我发疯:

<table>

<tr>
    <th>Chapter</th>
    @*<th class="woCoursewareFindTd">Page count</th>*@
    <th>Contents</th>
</tr>

@{
    foreach (var c in Model.Chapters)
    {
        if (c.Courseware2Id == c2.Courseware2Id)
        {
            <tr>
                <td>
                    @{

                        if (c.ChapterFileName.ToString().ToLower() == "none")
                        {
                            WriteLiteral(c.Courseware3Name);
                        }
                        else
                        {
                            <a href="@c.Href">@c.Courseware3Name (click to download)</a>
                        }
                    }
                    <p>(@c.PageCount page@(c.PageCount == 1 ? "" : "s"))</p>
                </td>

                <td>
                    @Html.Raw(c.SectionText)
                </td>
            </tr>
        }
    }
}

完美的!感谢上述所有 StackOverflow 贡献者。

For all the people whingeing about Visual Studio, I think it's pretty impressive that it allows you to switch between HTML and C# without being told which language you're using.

On a more practical note, I think my advice would be to combine lots of the things shown above. Specifically ...

  1. Avoid using @: to denote a literal string of HTML. Visual Studio frequently adds a line after it when you reformat your code, and even when it doesn't you can end up in an infinite recursion, using @ to then switch back to code, and so on. Use WriteLiteral for things not encoded in HTML tags, as suggested above; otherwise Visual Studio will detect HTML when you use a . If ...
  2. ... you use the fantastic idea of inserting code in a @{ ... } block.

Given these two I've found that CTRL K, D to reformat code gave perfect results for a table block which has been driving me mad:

<table>

<tr>
    <th>Chapter</th>
    @*<th class="woCoursewareFindTd">Page count</th>*@
    <th>Contents</th>
</tr>

@{
    foreach (var c in Model.Chapters)
    {
        if (c.Courseware2Id == c2.Courseware2Id)
        {
            <tr>
                <td>
                    @{

                        if (c.ChapterFileName.ToString().ToLower() == "none")
                        {
                            WriteLiteral(c.Courseware3Name);
                        }
                        else
                        {
                            <a href="@c.Href">@c.Courseware3Name (click to download)</a>
                        }
                    }
                    <p>(@c.PageCount page@(c.PageCount == 1 ? "" : "s"))</p>
                </td>

                <td>
                    @Html.Raw(c.SectionText)
                </td>
            </tr>
        }
    }
}

Perfect! Thanks to all StackOverflow contributors above.

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