Search code examples
visual-studio-2012editorcode-formattingrazor-2

Formatting options for Razor (cshtml) and C# (cs) files


Generally in my company we use MS C# Coding Conventions for C# code (.cs files). So, typically, our code looks more or less like this:

public class FooController
{
    public ActionResult Index()
    {
        for (int i = 0; i < 10; i++)
        {
            // Code here
        }

        if (true == false)
        {
            // More code here
        }

        return View();
    }
}

Which looks nice and readable.

But in my team, the designers (who usually work only on Razor cshtml files) use a different formatting in their code. More or less like this:

@using (Html.BeginForm()) {

    @if (true == false) {
        <p>You'll never see this!</p>
    }

}

They generally open curly braces in the same line and have other formatting options because they only use cshtml (Razor) files. (They do other tricks as well so the source code looks similar to generated HTML code.)

We have all agreed on that and it's cool.

The problem starts when I or some other developer edits part of the Razor view and Visual Studio re-formats the code using our (C#/cs file) settings. This is not desirable.

The question is:

Is there a way to have different formatting options for .cs and .cshtml files, or to stop Visual Studio from formatting Razor cshtml files specifically?

Any help will be kindly appreciated.


Solution

  • Visual Studio does not have such options.

    I'm using Telerik's JustCode. It has different formatting settings for C# and Razor. It also has some other useful for developers stuff.