Disable Visual Studio code formatting in Razor

后端 未结 9 1185
死守一世寂寞
死守一世寂寞 2020-12-13 17:13

This has been asked before: Why doesn't Visual Studio code formatting work properly for Razor markup?

But that question is a couple years old. And Razor formatti

相关标签:
9条回答
  • 2020-12-13 17:37

    Unfortunately the only way to prevent autoformatting on paste is to comment out the location where you're going to paste some new code and after the pasting operation to remove the commenting instructions.

    Search & Replace seems not to trigger auto-formatting. But pasting reformats the entire code block.

    This is terrible beyond words. It makes Visual Studio IDE virtually unusable for Razor editing. One has to use an external editor for Razor files.

    What's even worse, even the latest Visual Studio 2013 Update 1 crashes while editing Razor views all the time.

    0 讨论(0)
  • 2020-12-13 17:39

    You cannot. This is built in "feature" of VS since the first .net version (visual studio 2002/2003).

    There are tons of connect report about the formatting bugs, but Microsoft ignores them completly, or pushing the fix to the "next version". You can see an example Microsoft answer here.

    You can post your own feedback to Microsoft Team here

    0 讨论(0)
  • 2020-12-13 17:49

    The fun part is, you can disable C# formatting and HTML formatting but not Razor formatting.

    I've unchecked "Tools > Options > Text Editor > C# > Formatting > Automatically format on paste" and the same for HTML.

    Which results in the following fun experience in cshtml files:

    • Paste HTML code: no format
    • Paste C# code: no format
    • Paste Razor code: format everything wrong

    And since there is no option to change the Razor options, you have to live with this I guess.

    My "solution": paste and ctrl-z to undo the auto format

    0 讨论(0)
  • 2020-12-13 17:49

    I found these settings in Resharper 2019, which have improved my experience:

    0 讨论(0)
  • 2020-12-13 17:51

    The places to look:

    Tools -> Options -> Web Essentials -> HTML -> Auto-format HTML on Enter
    

    (Requires installation of Web Essentials extension)

    Set to False

    Tools -> Options -> Text Editor -> HTML -> Advanced -> Format on Paste
    

    Set to False

    With these settings I have no trouble in Visual Studio 2013.

    I used to have to press Ctrl-Z far too often, and if you forgot untangling the mess once your code was written and tested was a huge pain. I feel Microsoft could do alot to help users configure this as well as make it more accessible.

    0 讨论(0)
  • 2020-12-13 17:52

    I do not have a .Net Core project nor Blazor. So unfortunately I cannot test this. But a new formatting engine has been released by Microsoft for these languages.

    https://devblogs.microsoft.com/aspnet/new-experimental-razor-editor-for-visual-studio/

    Requires the latest edition of Visual Studio 2019.

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