Why doesn't Visual Studio code formatting work properly for Razor markup?

后端 未结 10 1652
醉梦人生
醉梦人生 2020-12-08 13:04

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 c

相关标签:
10条回答
  • 2020-12-08 13:25

    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:

    BEFORE

    AFTER

    0 讨论(0)
  • 2020-12-08 13:25

    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.

    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

    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

    0 讨论(0)
  • 2020-12-08 13:28

    In my case it was resharper overriding formatting options.

    If your using reshaper and getting this issue try this...

    Resharper >> Options >> Razor >> Editor & Formatting >> Untick “Auto-format on enter”

    0 讨论(0)
  • 2020-12-08 13:33

    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.

    0 讨论(0)
提交回复
热议问题