Disable Visual Studio code formatting in Razor

2019-02-02 19:03发布

问题:

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 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.

回答1:

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



回答2:

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



回答3:

The places to look:

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

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.



回答4:

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



回答5:

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.



回答6:

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



回答7:

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