C# “处理ASP.NET MVC”;标签汤;

C# “处理ASP.NET MVC”;标签汤;,c#,asp.net-mvc,visual-studio,markup,code-formatting,C#,Asp.net Mvc,Visual Studio,Markup,Code Formatting,今天我正在制作一个ASP.NET MVC模板,在盯着那些荧光黄%标签看了足够长的时间后,我基本上认为我已经受够了,所以我煞费苦心地修改了我的ascx文件,使其看起来像这样: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <% if (Model == null) {

今天我正在制作一个ASP.NET MVC模板,在盯着那些荧光黄
%
标签看了足够长的时间后,我基本上认为我已经受够了,所以我煞费苦心地修改了我的ascx文件,使其看起来像这样:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl"         %>

<%  if (Model == null)
    {                                                                       %>
<%=     ViewData.ModelMetadata.NullDisplayText                              %>
<%  }
    else if (ViewData.TemplateInfo.TemplateDepth > 1)
    {                                                                       %>
<%=     ViewData.ModelMetadata.SimpleDisplayText                            %>
<%  }
    else
    {                                                                       %>
<%      foreach (var prop in ViewData.ModelMetadata.Properties.Where(
            pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm)))
        {                                                                   %>
<%          if (prop.HideSurroundingHtml)
            {                                                               %>
<%=             Html.Display(prop.PropertyName)                             %>
<%          }
            else
            {                                                               %>
<%              if (!String.IsNullOrEmpty(prop.GetDisplayName()))
                {                                                           %>
                    <span class="display-label">
<%=                     prop.GetDisplayName()                               %>
                    </span>
<%              }                                                           %>
                <span class="display-field">
<%=                 Html.Display(prop.PropertyName)                         %>
                </span>
<%          }                                                               %>
<%      }                                                                   %>
<%  }                                                                       %>

1)
{                                                                       %>
pm.ShowForDisplay&!ViewData.TemplateInfo.Visited(pm)))
{                                                                   %>
啊,终于有了可读性。唯一的问题是,手工完成这项工作需要很长时间。我需要一种自动化的方法。某种代码格式化解决方案。可能是宏或Visual Studio加载项或…?你有什么建议

更新

我现在计划从我的标记中重构出大部分逻辑(参见下面Mike的回答),但与此同时,我提出了一种更易于管理的方式来格式化包含代码和html的ascx文件。这种方式的代码更分散一些,但一开始这样格式化代码要容易得多,使用起来也更容易

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<% 
    if (Model == null)
    {
%>
        <%= ViewData.ModelMetadata.NullDisplayText %>
<%
    }
    else if (ViewData.TemplateInfo.TemplateDepth > 1)
    {
%>
        <%= ViewData.ModelMetadata.SimpleDisplayText %>
<%
    }
    else
    {
%>
<%
        foreach (var prop in ViewData.ModelMetadata.Properties.Where(
            pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm)))
        {
            if (prop.HideSurroundingHtml)
            {
%>
                <%= Html.Display(prop.PropertyName) %>
<%
            }
            else
            {
%>
                <div class="display-row">   
<%
                if (!String.IsNullOrEmpty(prop.GetDisplayName()))
                    {
%>
                        <div class="display-label">
                            <%= prop.GetDisplayName() %>
                        </div>
<%
                }
%>
                    <div class="display-field">
                        <%= Html.Display(prop.PropertyName) %>
                    </div>
            </div>
<%
            }
        }
    }
%>

1)
{
%>
pm.ShowForDisplay&!ViewData.TemplateInfo.Visited(pm)))
{
if(项目隐藏周围TML)
{
%>

对于我们当前的项目,我们编写了一个HTML帮助程序,在一定程度上清理了所有的if-else:

public static void WriteIf(this HtmlHelper helper, bool condition, string truePart, string falsePart)
{
     helper.ViewContext.HttpContext.Response.Write(condition ? truePart : falsePart);
}
然后在HTML中,您会说:

<% Html.WriteIf(Model.IsTrue(), "TrueText", "FalseText"); %>

使用正确的视图模型(也可以选择使用spark view engine),没有标签汤

特别是,如果使用模板

关于这个例子,我说的不多(更不用说整理了),但对我来说,通常都是关于整洁感和正确组织事物的能力


“我正在寻找一个代码格式化解决方案。”=>然后你必须查看spark。它会“HTML化”你的视图,即使它们确实包含逻辑

您在it中的示例(无需重组任何内容)=>


${nullText}
${simpleDisplayText}
${Html.Display(prop.PropertyName)}
${prop.GetDisplayName()}
${Html.Display(prop.ProperyName)}
也许有什么地方错了,但你知道了。

我相信你的“标签汤”疼痛实际上是另一个问题的症状:你感觉到疼痛是因为你的视图中有逻辑。视图应该非常轻量级,几乎没有逻辑。你的逻辑应该在控制器中,控制器可以根据逻辑决定呈现不同的视图。或者你可以将逻辑放入帮助程序中


请看

这是一个很酷的主意,Matt,但是如果你有嵌套的或多行的
if
语句(如本例所示),它似乎并没有多大帮助。我正在寻找一个代码格式化解决方案。是的,同意。希望我能为你做些更好的事情。有趣的一点,谢谢。我使用的示例只是对n标准的ASP.NET MVC Object.ascx模板。我可以为我想要修改的所有模板编写帮助程序,但这不正是解决问题的方法吗(即,将html代码放在C#文件中,而不是html文件中的C#代码)?另一点:我现在正在对我的模板进行大量的尝试和错误,并将代码放在ascx文件中(与C#文件不同),我只需进行更改、保存文件并刷新浏览器即可。如果每次都要重新运行应用程序,这将耗费大量时间。也许一旦找到我喜欢的解决方案,为了可维护性,我可以编写助手,但现在,使用ascx文件是我的首选(只要它的格式与我的问题中的格式相同。)很多“官方”不幸的是,示例代码可能不是最佳实践。您通常可以将HTML代码放在片段中,并让助手呈现它们。助手可以实现C#逻辑,例如,它决定呈现哪个片段,和/或将适当的模型传递给片段。再次感谢,我想我同意您的想法,特别是结合使用帮助程序和部分功能的想法。Dan,还有另外两个技巧。你不必每次更改代码都重新运行网站。只需运行网站一次,然后打开网站的另一个浏览器选项卡/窗口并保持其打开状态。当你更改代码时,只需进行生成并刷新浏览器窗口。你不必运行第二,在“项目选项”的“web”选项卡上,将“开始”选项更改为“不打开页面…”调试时,只需点击“运行”并刷新页面。第一次,运行,然后右键单击ASP.NET开发服务器并选择“在web浏览器中显示”@Kirk,是代码还是格式?哦:你有没有想过在编码程序中维护它!?呵呵:P
<var nullText="ViewData.ModelMetadata.NullDisplayText"
     templateDepth="ViewData.TemplateInfo.TemplateDepth"
     simpleDisplayText="ViewData.ModelMetadata.SimpleDisplayText"
     props="ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))" 
     m="ViewData.Model"/>
<if condition="m==null">
  ${nullText}
</if>
<elseif condition="templateDepth>1">
  ${simpleDisplayText}
</elseif>
<else>
  <for each="var prop in props">
    <if condition="prop.HideSurroundingHtml">
      ${Html.Display(prop.PropertyName)}
    </if>
    <else>
      <span if="!string.IsNullOrEmpty(prop.GetDisplayName()" class="display-field">
        ${prop.GetDisplayName()}
      </span>
      <span class="display-field">
        ${Html.Display(prop.ProperyName)}
      </span>
    </else>
  </for>
</else>