Disable Visual Studio code formatting in Razor

Visual StudioRazorCode Formatting

Visual Studio Problem Overview


This has been asked before: https://stackoverflow.com/questions/6902204/why-doesnt-visual-studio-code-formatting-work-properly-for-razor-markup

But that question is a couple years old. And Razor formatting is still completely unacceptable. I have given up on fixing it.

Can I disable ALL Visual Studio formatting for CSHTML files? If not, can I disable all Visual Studio formatting entirely? Any 3rd party hacks I can use? There's got to be something!

I've upgraded to 2013 and the formatting is just as bad.

[EDIT]: 2015 is god-awful as well. I REALLY wish they would fix this.

[EDIT]: 2017 is god-awful as well. I REALLY wish they would fix this.

[EDIT]: 2019 is god-awful as well. I REALLY wish they would fix this. That said, Microsoft released a new formatting engine for .Net Core only.

[EDIT]: 2022 is... Well you know. If they ignore this 12 more years I may retire before seeing it fixed!

Visual Studio Solutions


Solution 1 - Visual Studio

Under Tools -> Options -> Text Editor -> HTML -> Advanced

there is a key value item Paste which says "Format on paste" and has a boolean value next to it. Changing this to false has disabled formatting on paste for me in razor syntax.

I am using VS Professional 2013, Version 12.0.30110.00 Update 1

Solution 2 - Visual Studio

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

Solution 3 - Visual Studio

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.

Solution 4 - Visual Studio

What did the trick for me was pressing CTRL+Z after pasting, then the formatting is removed.

Solution 5 - Visual Studio

In my case R# ended up being the culprit, can be disabled here:

ReSharper > Options > Code Editing > Razor > Editor & Formatting

https://www.jetbrains.com/help/resharper/2016.1/Reference__Options__Languages__Razor__Editor.html

Solution 6 - Visual Studio

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.

Solution 7 - Visual Studio

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

Solution 8 - Visual Studio

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.

Solution 9 - Visual Studio

I found these settings in Resharper 2019, which have improved my experience: enter image description here

Solution 10 - Visual Studio

The below does not directly answer the posed question, but offers a potential fix that may be helpful.

In my situation, the auto formatting was only preforming exceptionally poorly for some files. This turned out to be related to the type of line feed used.

To resolve:

  • I enabled advanced save options
  • When saving the problematic files, I selected Windows (CR LF) as the line feed type

Supporting docs

After doing this, autoformat appeared to behave better

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaulView Question on Stackoverflow
Solution 1 - Visual StudioDroydnView Answer on Stackoverflow
Solution 2 - Visual StudioKuzgunView Answer on Stackoverflow
Solution 3 - Visual StudiodazbradburyView Answer on Stackoverflow
Solution 4 - Visual Studiouser2920118View Answer on Stackoverflow
Solution 5 - Visual StudiohcoverlambdaView Answer on Stackoverflow
Solution 6 - Visual StudioguestView Answer on Stackoverflow
Solution 7 - Visual StudioChristiaan MaksView Answer on Stackoverflow
Solution 8 - Visual StudioPaulView Answer on Stackoverflow
Solution 9 - Visual StudioStephen OberauerView Answer on Stackoverflow
Solution 10 - Visual StudioChristopher ThomasView Answer on Stackoverflow