Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent visual studio auto-indenting C# code in ASP.Net MVC Views

I am in a view editing HTML and I may for example need one line of C# code inside <% %> tags..

If that C# code contains a bracket like { or } after I press enter or complete the command, VS automatically spreads the line over 2 lines to kind of auto-indent the brackets.

It's really annoying because I am not in code view, I just want my view to look super clean and therefore keep the C# indentation exactly how I typed it in.

Any idea how to disable this???

Many thanks..

like image 326
Aaron Avatar asked Nov 06 '10 02:11

Aaron


2 Answers

Every Visual Studio auto-indent operation is being saved in the Undo history. This means that if you press Ctrl+Z (undo) immediately after the auto-indent, it will be canceled and the code will be reverted to the initial indentation. This undo operation will not affect any characters that you have entered, only the auto-indentation.

It is still annoying, since it requires an additional keyboard input, but for many situations it is a good enough workaround.

like image 95
Florin Dumitrescu Avatar answered Sep 28 '22 00:09

Florin Dumitrescu


Yeah, I've experienced that too... Hate it when VS does that. But the only way to do it, that I know of, is disabling C#'s "Automatically format completed block on }"...

Go to Tools | Options | Text Editor | C# | Formatting | General and uncheck it.

BUT you will lose this feature for all C# code (not only in views)...

like image 33
rsenna Avatar answered Sep 28 '22 02:09

rsenna