为什么 Visual Studio 代码格式不能正确用于 Razor 标记? [英] Why doesn't Visual Studio code formatting work properly for Razor markup?

查看:35
本文介绍了为什么 Visual Studio 代码格式不能正确用于 Razor 标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者,我应该问一下,VS 代码格式何时才能对 Razor 标记正常工作?格式化适用于大多数结构,但它似乎在 'if' 块上窒息.下面的代码是由 VS 格式化的.修复这种情况很容易,再缩进一个,但我很好地接受了日常使用中的格式,并且喜欢在我的大部分代码中经常使用它,所以如果可能的话,我宁愿避免手动格式化.现在我只是将其保留为 VS 格式.

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.

@{ 
    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;" })
    }
 }

我认为可读性很重要,例如在上面,if 块的主体是缩进的,除了看起来更好.

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.

推荐答案

确保将编辑器设置为使用空格字符而不是制表符.使用选项卡时,编辑器似乎完全失去了理智.这是一种耻辱,因为所有这些空格字符最终都会出现在实际的 HTML 输出中,从而大大增加了数据传输的大小.我所做的是在键入时手动补充自动格式.不理想,但希望微软能在下一个服务包中解决这个问题.

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.

这篇关于为什么 Visual Studio 代码格式不能正确用于 Razor 标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆