I'd like Visual Studio to continue automatically formatting my .cs files, but is there a way to prevent it auto-formatting C# code in between the <% %> tags in ASP.NET MVC views because it really makes a mess of it?
-
1+1. That's incredibly frustrating!Drew Noakes– Drew Noakes2010-10-06 00:32:31 +00:00Commented Oct 6, 2010 at 0:32
-
Has anyone tried this with VS2010 SP1 yet? The beta was released in the last few days.Drew Noakes– Drew Noakes2010-12-10 02:09:49 +00:00Commented Dec 10, 2010 at 2:09
-
@Drew - not yet. If I get time I may fire up a VM and test this.Kev– Kev2010-12-10 09:31:11 +00:00Commented Dec 10, 2010 at 9:31
4 Answers
As far as I know there isn't a way to turn off the formatting just in ASP.NET files as it uses the rules for C# in Tools -> Options -> Text Editor -> C#.
1 Comment
There is no way to do it and it is a huge time waster fixing up the formatting in .aspx pages over and over again. It drives me nuts.
There are two connect issues/bugs for this. One was marked as fixed for months but the latest response does not look like anything was even done.
Comments
Unfortunately (afaik), you can't have separate settings for C# in .cs and .aspx files. You can change the settings for C# in Tools -> Options -> C# -> Formatting.
To get things to work well with MVC, I changed some settings in the New Lines section. I unchecked the boxes for control block, object initializers, and "else". This produces the walk-like-an-egyptian style braces that works best in MVC code, without screwing too much with my normal C# code. I leave the settings for functions, classes etc. alone since you don't normally define those in an .aspx file.
1 Comment
You may consider using Razor as your View Engine instead of aspx.
see also:
- http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx (Scott Guthrie)
- ASP.NET MVC Razor view engine (StackOverflow)