Razor Helper Syntax Auto Formatting Ugly. How to fix?

后端 未结 5 803
盖世英雄少女心
盖世英雄少女心 2021-01-07 15:56

So I just have a beef with the way Visual Studio formats razor code. I\'ve always had some problems with visual studio and how it formats UI code, it always seems to do a re

5条回答
  •  执念已碎
    2021-01-07 16:43

    For all the people whingeing about Visual Studio, I think it's pretty impressive that it allows you to switch between HTML and C# without being told which language you're using.

    On a more practical note, I think my advice would be to combine lots of the things shown above. Specifically ...

    1. Avoid using @: to denote a literal string of HTML. Visual Studio frequently adds a line after it when you reformat your code, and even when it doesn't you can end up in an infinite recursion, using @ to then switch back to code, and so on. Use WriteLiteral for things not encoded in HTML tags, as suggested above; otherwise Visual Studio will detect HTML when you use a . If ...
    2. ... you use the fantastic idea of inserting code in a @{ ... } block.

    Given these two I've found that CTRL K, D to reformat code gave perfect results for a table block which has been driving me mad:

    
        @**@
        
    
    @{
        foreach (var c in Model.Chapters)
        {
            if (c.Courseware2Id == c2.Courseware2Id)
            {
                
            }
        }
    }
    

    Perfect! Thanks to all StackOverflow contributors above.

    提交回复
    热议问题
    ChapterPage countContents
    @{ if (c.ChapterFileName.ToString().ToLower() == "none") { WriteLiteral(c.Courseware3Name); } else { @c.Courseware3Name (click to download) } }

    (@c.PageCount page@(c.PageCount == 1 ? "" : "s"))

    @Html.Raw(c.SectionText)