处理 ASP.NET MVC“标签汤”
我今天正在开发一个 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>
<% } %>
<% } %>
<% } %>
啊,终于可读了。唯一的问题是,手动完成此操作需要很长时间。我需要一种方法来自动化此操作。某种代码格式化解决方案。也许是宏或 Visual Studio 加载项或...?你有什么建议?
更新
我现在计划重构标记中的大部分逻辑(请参阅下面 Mike 的回答),但与此同时,我想出了一种更易于管理的方法来格式化具有以下内容的 ascx 文件:代码和 html 的混合体。这样代码会更加分散,但是首先像这样格式化代码要容易得多,而且使用起来也容易得多。
<%@ 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>
<%
}
}
}
%>
I was working on an ASP.NET MVC template today, and after staring at all those fluorescent yellow %
tags for long enough, I basically decided I had had enough, so I painstakingly modified my ascx file to look like this:
<%@ 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>
<% } %>
<% } %>
<% } %>
Ahh readability at last. The only problem is, it takes way to long to do this by hand. I need a way to automate this. Some kind of code formatting solution. Perhaps a macro or a Visual Studio add-in or ...? What do you advise?
Update
I'm now planning to refactor out most of the logic from my markup (see Mike's answer below), but in the mean time, I came up with a more manageable way to format ascx files that have a mixture of code and html. The code is a little more spread out this way, but it's much easier to format the code like this in the first place, and it's much easier to work with as well.
<%@ 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>
<%
}
}
}
%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信你的“标签汤”痛苦实际上是另一个问题的症状:你感觉到它是因为你的观点有逻辑。视图应该非常轻量级,很少或没有逻辑。您的逻辑应该位于控制器中,控制器可以根据逻辑决定渲染不同的视图。或者你可以将逻辑放入助手中。
请参阅Rob Conery 撰写的这篇文章
I believe your "tag soup" pain is actually a symptom of a different problem: you're feeling it because you've got logic in your view. Views should be very lightweight, with little or no logic. Your logic should be in your controllers, which can decide to render different views depending on the logic. Or you can put the logic into helpers.
See this article by Rob Conery
有了正确的视图模型(也可以选择使用 Spark 视图引擎),就不存在标签汤了。
特别是,如果使用模板。
关于这个例子不能说太多(甚至更少 - 整理它),但对我来说 - 通常这都是关于整洁感和正确构建事物的能力。
“我正在寻找代码格式化解决方案。” =>那么你必须检查火花。它会“html 化”您的视图,即使它们确实包含逻辑。
你的例子(没有重组任何东西)=>
可能有些地方是错的,但你明白了。
With proper view model (and optionally, with spark view engine), there is no tag soup.
Particularly, if templates are used.
Can't tell much about this example (even less - to sort it out), but for me - usually it's all about sense of tidiness and ability to structure things properly.
"I'm looking for a code formatting solution." => then you must check out spark. It 'htmlifies' your views even if they do contain logic.
Your example in it (without restructuring anything) =>
Might be wrong somewhere, but you got the idea.
对于我们当前的项目,我们通过编写 HTML 帮助程序来清理所有的 if-else:
然后在 HTML 中,您会说:
For our current project, we cleaned up all the if-elses somewhat by writing an HTML helper:
and then in the HTML, you would say: