MVC3 Razor RC when auto formatting document causes invalid code block indention - by cyost

Status : 


Sign in
to vote
ID 621084 Comments
Status Active Workarounds
Type Bug Repros 28
Opened 11/11/2010 11:10:44 AM
Access Restriction Public


If I have some code in a cshtml file, and I format it, I get code that looks like this:

          @if (Model.IsSomething) 
              <span>Some Text</span>
              <span>Some Other Text</span>

Instead of:

          @if (Model.IsSomething) 
              <span>Some Text</span>
              <span>Some Other Text</span>

It also puts some spaces and tabs in front of the code blocks, even if I have it set to use tabs.  None of this happens in normal C# files.
Sign in to post a comment.
Posted by rich79 on 11/29/2016 at 5:50 AM
I can put up with a little wacky formatting, I can not accept it breaking stuff by adding spaces in all my action names. So...
<a href="@Url.Action("Add", "User")">
    <i class="fa fa-plus-circle"></i>
gets changed to...
<a href="@Url.Action(" Add", "User")">
    <i class="fa fa-plus-circle"></i>

This is critical as it breaks functionality! Quite easily missed and will compile unless your building views which is painfully slow for CI. Luckily it was picked up in QA before it got live and fubar'd my site.

Now I have to remember every time I edit the file that all my actions will get screwed and I need to fix them.

As razor formatting seems to have been an issue for a long time and seemingly impossible to fix, please give us an option to turn the damn thing off!
Posted by stmarti1 on 8/22/2016 at 3:36 AM
Sorry I was too quick. Formatting still has bugs. We have no longer get spaces when the indenting is set to tab, this is great. However nested razor blocks' brackets may format incorrectly.


@if( true )
    <div>@{ int a; a = 0; }</div>

formatted as:

@if( true )
    <div>@{ int a; a = 0; }</div>

Posted by stmarti1 on 7/27/2016 at 2:23 AM
Seems finally fixed in vs 2015 update 3 (at least for me).

My settings:
Text editor\C#\Tabs : smart indenting, tab size 3, keep tabs.
Text editor\HTML\Tabs : smart indenting, tab size 3, keep tabs.

I see no more spaces at all just tabs, everything indented perfect when starting a new line, pasted code formatted correct. I just cut and past the wrong indented example from this bug report in to a very complex razor code block and was indented/formatted fine.

@AnnetteVDeutschland maybe you can check your installed version, mine:
VS 14.0.25424.00 Update 3
Microsoft ASP.NTE Web frameworks and tools: 5.2.40314.0

Posted by AnnetteVDeutschland on 7/7/2016 at 2:40 AM
Not fixed in VS 2015 update 3.
ReSharper was a great scrap, too. Because it destroyed my keyboard layout.

In the moment I don't know, how I have to work with VS 2015 again.

Is there the devil, because it makes the tab indent to column "7"?

It is not possible to comment some lines without crapped autoformat of VS. Ctrl-Z does not function, too.
Posted by stmarti1 on 4/27/2016 at 1:34 AM
Not fixed in vs 2015 update 2
Posted by abbottdev on 4/18/2016 at 3:22 PM
Seconded, I'm receiving this behavior in VS 2015 Update 1. I've also set my tabs to use spaces, and even tried enabling block indentation too to no avail.
Posted by Bernhard Jung on 2/24/2016 at 4:38 AM
I also experience problems with Visual Studio 2015 formatting Razor code. Indentation is set to Spaces but that doesn't help anything.
Posted by WalterE on 12/10/2015 at 12:19 AM
The problem still exists in VS 2015 update 1. Is there any indication of when this will be solved?
Posted by stmarti1 on 12/3/2015 at 5:55 AM
Not fixed in vs 2015 Update 1 (for any MVC, and version with razor)
Posted by astrosteve99 on 8/21/2015 at 2:01 PM
Visual Studio 2013, this problem is BAD! I can't believe you are unable to fix this Microsoft. I assume because you don't care and aren't trying. As a programmer, I always say ANYTHING can be done. Some things are just harder than others. Indenting a file doesn't seem very difficult. If you can't do it, then quit trying - it is so frustrating to have my properly indented file to be jerked all around so it looks horrible!
Posted by Piotr Kula on 5/8/2014 at 7:58 AM
Using VS2013 it is much better than it used to be in VS2012. But in razor when I do a mix of using code, and @ escaped code it makes the auto indent/ format go a bit crazy. I notocied though, if I open curl bracket and put the first line of code right next to the bracket it formats much better for example.

@if ( 0 == 0) { string seeSharp = "isAwesone";
string something = "two";

it will aling properly, I can live with the first line of code sitting next to the bracket on the View...
Posted by stmarti1 on 11/13/2013 at 4:21 AM
I was too quick, the new editor is better, but buggy as hell. (Still inserts spaces sometimes when formatting.)

Can't believe why is this so hard to implement...
Posted by stmarti1 on 10/18/2013 at 3:59 AM
This 10 years old ( editor not supported correct tab formatting from the very first version) bug finally fixed for me in vs 2013 RTM.
Works only in cshtml, so the bug remains(?) for aspx files.

Also for existing projects we need to migrate to mvc5 first (

An existing mvc project without migration kicks in the old webform editor.
Posted by tbau on 7/9/2013 at 1:04 PM
Any update on this nearly 3 year old bug?
Posted by Van [MSFT] on 5/20/2013 at 9:06 AM
We are working on resolving this issue in the current version of Razor and VS tooling under development.
Posted by Derek Kalweit on 2/6/2013 at 9:50 AM
Not fixing this is unacceptable. "Use spaces instead of tabs" is clearly an answer from someone who doesn't understand that tabs are clearly better-- providing the option(tabs versus spaces) is the only reasonable option for a competent company(something Microsoft is(was?)). Failure is not an option-- FIX IT!!!!!!!!
Posted by Joshua R. Mouch on 1/17/2013 at 9:34 AM
To copy a previous poster...
It's the year 2013 and this still isn't fixed.
Posted by Joshua R. Mouch on 1/17/2013 at 9:34 AM
Yes, please fix. I hate using space indentation. I could argue all day why tab indentation should be the Visual Studio default.
Posted by spongman on 10/19/2012 at 11:04 AM
ridiculous, Jeff Handley.
Posted by stmarti1 on 8/30/2012 at 12:45 PM
Still not fixed in mvc4 and in vs 2012. Why was this closed???
This is a serious bug, using tabs and the document not formatting correctly. This is not even razor specific, this bug present since the first vs version (similar problems in <% %> blocks!)!
Posted by jpdedmon on 3/1/2012 at 8:01 AM
hope this was considered in the new VS.
Posted by Microsoft on 2/15/2012 at 2:31 PM
Unfortunately, we won't be able to fix this issue. We have investigated potential fixes, and each causes regressions elsewhere that are worse than this issue.

The workaround is to use Spaces rather than keeping Tabs. We apologize for the inconvenience on this and we regret that we cannot confidently implement a fix.

Best regards,
Jeff Handley
Posted by GeekyMonkey on 2/15/2012 at 1:16 AM
It's like there's a steering wheel in my pants. It's driving me nuts.

Anyway.... If a fix isn't on the way, it would be nice to know. In that case maybe somebody could create a plugin to clean these up. Not saying it shouldn't be fixed. It certainly should. I'd just like to know if/when it might happen.
Posted by b j z on 2/10/2012 at 5:00 PM
This is ridiculous...fix this problem!
Posted by Fahad Khalil on 2/2/2012 at 2:52 AM
Its the year 2012 - and still no fixes! annoying and pretty disappointing isn't it?
Posted by creature- on 12/19/2011 at 5:21 PM
Still not fixed
Posted by Jimmy Ross on 10/24/2011 at 9:08 AM
Is this ever going to be fixed?
Posted by wordemup on 10/18/2011 at 10:47 AM
Posted by Court Newman on 7/27/2011 at 8:50 PM
I can confirm that this bug is still exists. Very very annoying. Please MS fix this. Thank you.
Posted by cyost on 7/7/2011 at 5:59 PM
Why does this keep getting closed when SEVERAL people still see the issue. Is there a hotfix? This is needed badly as all of our developers have to keep reformating the cshtml files.
Posted by cyost on 6/7/2011 at 3:59 PM
This continues to happen. MAKE SURE YOU HAVE SAVE TABS (not spaces) FOR THE FILE. The indention places a SPACE for every TAB...
Posted by Gonzalo Calandria on 6/3/2011 at 5:54 AM
I am also having the same issue even with SP1 and i try to with some foreach statements and it's the same problem.
This is very anoying because i have to spend almost 2 minutes trying to found every close character.
Posted by samuel.abj on 5/19/2011 at 7:21 PM
I forgot to mention that specifying Tab - "Insert spaces" works fine.
Posted by samuel.abj on 5/19/2011 at 7:19 PM
I can confirm this problem still exists in SP1, Version 10.0.40219.1 SP1Rel
Options - Text Editor - Tabs - Keep tabs

        @if(true) {
     <span>Why oh why is this happening</span>
Posted by Peter Zo on 5/5/2011 at 10:50 AM
We have it set to use spaces, rather than tabs, and we still experience this issue. It seems to happen more when using switch statements or with if/else. The way it handles the "break" inside a switch is especially broken. I align it two characters away from switch, add one more case statement and the previous break is moved to the right, I add a few more blocks and it moves them around again.
Posted by stmarti1 on 3/16/2011 at 8:31 AM
This was NOT fixed in sp1. Same issue in vs 2010 pro sp1 and mvc 3 rtm.
All custom c# formatting applied to razor syntax, except identing: formatting always use spaces :(
Posted by mike_lockyer on 3/15/2011 at 9:56 AM
I am also having the same issue with SP1 installed
If I set HTML formatting to be spaces the layout is good
If I set to use Tabs (our standard) layout is a mess
Posted by cyost on 3/9/2011 at 7:04 AM
I just tried this, and it still doesn't work. It is better (meaning that putting the end brace does not badly reformat the whole document), but it still formats incorrectly in my case.

Our standard is to "Save Tabs" for html files, and when I do that, the formatting still puts spaces (number is the same as the number of tabs) which causes it to badly indent.

Below is a very small example after reformatting.

    ViewBag.Title = "ViewPage1";

    ViewBag.Title = "ViewPage1";
            @if (0 == 1)
                    Row one
                Row 2

Notice the open and close braces on the if's are still indented incorrectly.
Posted by Sean [MSFT] on 2/4/2011 at 8:08 PM
Hi Cyost,

Thank you for reporting this issue. I can confirm that it is indeed fixed in the upcoming SP1 of Visual Studio 2010.

Thank you for helping improve Visual Studio.
-Sean Laberee
Posted by Deepak [MSFT] on 11/15/2010 at 12:41 PM
Thanks for your quick response. I was able to repro the issue and have raised a bug.
Posted by cyost on 11/15/2010 at 8:49 AM
I attached a zip file containing a cshtml (since cshtml files are not allowed to be attached) that has the problem.

There is an @if on line 48 that gets moved radically to the right
There is an @if on line 55 that doesn't move, but the { and } (line 56 and 59) move radically to the right.

The only real change to the settings is we keep tabs instead of spaces in our documents.
Posted by Deepak [MSFT] on 11/12/2010 at 12:53 PM
Thanks Cyost for reporting this issue. I was not able to repro the formatting that you are seeing. I noticed another issue where the second <span> in the else is not formatted correctly. This has been fixed for the next release.

Can you please attach your cshtml file that doesn't format correctly so that we can try in our labs to get a repro. Also it would be great to know if you have changed any of the default settings in Tools->options -> Text editor -> C#

SDET, Web Platform & Tools
Posted by Microsoft on 11/11/2010 at 9:03 PM
Thanks for your feedback.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 11/11/2010 at 11:21 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(