Disable Visual Studio code formatting in Razor

2019-02-02 19:10发布

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.

7条回答
聊天终结者
2楼-- · 2019-02-02 19:21

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楼-- · 2019-02-02 19:25

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.

查看更多
来,给爷笑一个
4楼-- · 2019-02-02 19:30

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

查看更多
时光不老,我们不散
5楼-- · 2019-02-02 19:34

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.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-02-02 19:34

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

查看更多
叛逆
7楼-- · 2019-02-02 19:37

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

查看更多
登录 后发表回答