views:

48

answers:

1

Hello,

MVC is formatting my code horribly, and I was wondering if you can turn it off? I feel the answer is no, but I was hoping VS 2010 had built in a setting...

Here's what its formatting as:

                  <% if (org.UserKey.HasValue)
{ %>

                        <%= org.Reference(i => i.UserReference).Email%>

                  <% }
else
{ %>

                        <%= org.UserEmail%>

                  <% } %>

I want the beginning brackets on the same line as the if and the else...

Thanks.

+1  A: 

You can indeed set this.

In Visual Studio, go to Tools -> Options.

In the treeview on the left, navigate to Text Editor -> C# -> Formatting -> New Lines. You can uncheck the checkbox for "Place open brace on new line for control blocks."

Unfortunately, this will also change it for all of your *.cs files as well.

Another option to clean it up a bit is to change your "<%=" blocks to Response.Write. That way, you can avoid having so many opening and closing <% tags, as follows:

 <% if (org.UserKey.HasValue)
    {
        Response.Write(org.Reference(i => i.UserReference).Email);
    }
    else
    {
        Response.Write(org.UserEmail);
    } %>

As one final side note, if you're using .NET 4.0, you should use <%: instead of <%= from now on. That Html-encodes your output so you can easily injection attacks. It is the same thing as Response.Write(HttpUtility.HtmlEncode(expression)).

Nate Dudek