如何标记以及ASP.NET MVC抽象的共同片段 [英] How to abstract common snippets of markup with ASP.NET MVC

查看:222
本文介绍了如何标记以及ASP.NET MVC抽象的共同片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多在我的ASP.NET MVC 2网站内容为主的观点。这些含有几种再次发生HTML模式。当使用ASP.NET Web表单,自WebControl派生的类可以封装这些模式。我想对这个问题,MVC正确的做法一些指点。

I have a lot of content-heavy views in my ASP.NET MVC 2 site. These contain several re-occurring HTML patterns. When using ASP.NET Webforms, a class derived from WebControl could encapsulate these patterns. I'd like some pointers on the correct approach for this problem with MVC.

详解

模式不是不像下面的HTML标记保持在这些观点的发生。标记渲染成内容的一个孤立的一个盒子:

Patterns not unlike the following HTML markup keep occurring throughout these views. The markup renders into an isolated a box of content:

<div class="top container">
    <div class="header">
       <p>The title</p>
       <em>(and a small note)</em>
    </div>
    <div class="simpleBox rounded">
       <p>This is content.</p>
       <p><strong>Some more content</strong></p>
    </div>
</div>

这是一个简单的例子,但也有更复杂的重复模式。在ASP.NET Web表单,我会等抽象code到WebControl的(比方说我已经把它命名为BoxControl),被包括在页面像这样的:

This is a trivial example, but there are more complex recurring patterns. In ASP.NET Webforms I would have abstracted such code into a WebControl (let's say I'd have named it BoxControl), being included on a page like this:

<foo:BoxControl runat="server">
  <Header>The title</Header>
  <Note>(and a small note)</Header>
  <Content>
     <p>This is content.</p>
     <p><strong>Some more content</strong></p>
  </Content>
</foo:BoxControl>

这使得抽象很容易适应整个网站的构造与框的方式,仅通过改变BoxControl源。它也保持了静态HTML内容一起整齐地在查看页面,在页面上结合几个BoxControls时也是如此。另一个好处是,用作内容的HTML是由IDE识别,从而提供语法高亮/检查

This abstraction makes it easy to adapt the way the box is constructed throughout the site, by just altering the BoxControl source. It also keeps the static HTML content neatly together in the View Page, even when combining several BoxControls on a page. Another benefit is that the HTML used as content is recognized by the IDE, thus providing syntax highlighting/checking.

据我了解,器WebControls不鼓励在ASP.NET MVC。取而代之的是WebControl的,我可以完成使用局部视图的抽象。那么这种观点将被包含在视图页面如下:

To my understanding, WebControls are discouraged in ASP.NET MVC. Instead of a WebControl, I could accomplish the abstraction with a partial view. Such a view would then be included in a View Page as follows:

<%= Html.Partial("BoxControl", new {
  Header="The Title", 
  Note="(and a small note)", 
  Content="<p>This is content.</p><p><strong>Some more content</strong></p>"});
%>

这是不理想的,因为内容参数可以变得很长,而当通过这种方式,IDE不会把它当作HTML。

This is not ideal, since the 'Content' parameter could become very long, and the IDE does not treat it as HTML when passed this way.

考虑解决方案
强类型的ViewModels可以传递给Html.Partial调用,而不是上面显示的冗长的参数。但后来我不得不从别的地方拉内容(一个CMS,或资源文件)。我想要的内容要包含在查看页面。

Considered Solutions Strongly-Typed ViewModels can be passed to the Html.Partial call instead of the lengthy parameters shown above. But then I'd have to pull the content in from somewhere else (a CMS, or Resource file). I'd like for the content to be contained in the View Page.

我也有考虑的杰弗里·巴勒莫提出的解决方案,但是这将意味着大量散落在项目额外的文件。我想任何视图中的文本内容被限制到只有一个文件。

I have also considered the solution proposed by Jeffrey Palermo, but that would mean lots of extra files scattered around the project. I'd like the textual content of any view to be restricted to one file only.

我应该不想抽象的标记了?或者是有可能的方法,适用于MVC,那我在这里可以俯瞰?什么是犯罪的缺点,通过使用WebControl的?

Should I not want to abstract the markup away? Or is there maybe an approach, suitable for MVC, that I am overlooking here? What is the drawback to 'sinning' by using a WebControl?

推荐答案

考虑到答案和运行实验后,我倾向于坚持以纯MVC方法和重复整个浏览一些presentation code页面。我想详细说明作出这一决定的理由。

After considering the answers and running an experiment, I'm inclined to adhere to the pure MVC approach and duplicate some presentation code throughout View Pages. I'd like to elaborate on the rationale for that decision.

管窥
当使用的局部视图,需要为框中的内容作为视图模型传递,使得浏览网页的可读性与当场宣布内容的HTML。请记住,内容并非来自一个CMS,因此这将意味着填充HTML视图模型中的一个控制器或设置在查看页面的局部变量。这两种方法都不能利用IDE功能处理HTML。

Partial View When using a Partial View, The content for the box needs to be passed as a View Model, making the View Page less readable versus declaring the content HTML on the spot. Remember that the content does not come from a CMS, so that would mean filling the View Model with HTML in a controller or setting a local variable in the View Page. Both of these methods fail to take advantage of IDE features for dealing with HTML.

的WebControl
在另一方面,WebControl的派生类是灰心,也证明有一些实际问题。主要的问题,传统的ASP.NET .aspx页的声明,分层风格只是不适合MVC.NET查看网页的程序风格。你必须选择为任何一个完全成熟的传统方法,或者去完全MVC。

WebControl On the other hand, a WebControl-derived class is discouraged and also turns out to have some practical issues. The main issue that the declarative, hierarchical style of traditional ASP.NET .aspx pages just does not fit the procedural style of MVC.NET View Pages. You have to choose for either a full blown traditional approach, or go completely MVC.

要说明这一点,在我的实验实现最突出的问题是变量范围之一:迭代的产品列表时,MVC-方法是使用一个的foreach 环,但是这引入了一个局部变量,它不会在WebControl的范围是可用的。传统的ASP.NET的方法是使用一个中继器,而不是的foreach 。这似乎是一个滑坡使用任何传统的ASP.NET控件可言,因为我怀疑,你很快就会发现自己需要整合越来越多的人来完成这项工作。

To illustrate this, the most prominent issue in my experimental implementation was one of variable scope: when iterating a list of products, the MVC-way is to use a foreach loop, but that introduces a local variable which will not be available in the scope of the WebControl. The traditional ASP.NET approach would be to use a Repeater instead of the foreach. It seems to be a slippery slope to use any traditional ASP.NET controls at all, because I suspect you'll soon find yourself needing to combine more and more of them to get the job done.

纯HTML
放弃抽象可言,如果只剩下重复presentation code。这是对干燥,而且产生了非常可读code。

Plain HTML Forgoing the abstraction at all, you are left with duplicate presentation code. This is against DRY, but produces very readable code.

这篇关于如何标记以及ASP.NET MVC抽象的共同片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆