Is there a way to prevent Visual Studio 2010 formatting C# code in ASP.NET MVC views?
Asked Answered
I

4

9

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?

Incomplete answered 30/9, 2010 at 22:3 Comment(3)
+1. That's incredibly frustrating!Scouting
Has anyone tried this with VS2010 SP1 yet? The beta was released in the last few days.Scouting
@Drew - not yet. If I get time I may fire up a VM and test this.Incomplete
Q
3

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

Quell answered 6/10, 2010 at 0:26 Comment(1)
Maybe in v.next they'll fix this.Incomplete
M
2

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.

https://connect.microsoft.com/VisualStudio/feedback/details/514691/add-aspx-or-inline-c-as-an-option-in-text-editor-formatting-options

https://connect.microsoft.com/VisualStudio/feedback/details/586729/support-c-formatting-options-for-aspx-pages

Marcelline answered 10/12, 2010 at 0:39 Comment(0)
F
1

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.

Fowkes answered 8/10, 2010 at 18:4 Comment(1)
Thanks, I'll have a fiddle about with these settings.Incomplete
C
-1

You may consider using Razor as your View Engine instead of aspx.

see also:

Coed answered 31/5, 2011 at 11:9 Comment(2)
Makes no difference to the formatting problem with VS - it makes a hash of Razor views too.Rabbi
@JonathanMoffatt if you use spaces instead of tabs you should be fine: #6267665Coed

© 2022 - 2024 — McMap. All rights reserved.