开发者

VS 2010 MVC Formatting

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 t开发者_Python百科he same line as the if and the else...

Thanks.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜