我们什么时候应该使用HTML辅助,剃刀助手或部分景致? [英] When should we use Html Helpers, Razor Helpers or Partial Views?

查看:84
本文介绍了我们什么时候应该使用HTML辅助,剃刀助手或部分景致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这三种不同的特点,在剃刀视图发动机存在,并且可以用于实现类似的结果。最终三人都只是呈现HTML code片,但方式来定义,并使用它们是相当不同的。我知道:

These three different features exist in the Razor view engine and can be used to achieve similar results. In the end all three of them just render pieces of HTML code, but the way to define and use them is fairly different. I know that:

HTML辅助作为扩展方法的的HtmlHelper 类创建。他们经常使用 TagBuilder 类生成一些HTML,总是应该返回一个 IHtmlString

Html Helpers are created as extension methods for the HtmlHelper class. They frequently use the TagBuilder class to generate some HTML and always should return an IHtmlString.

剃须刀助手( @helper 方法)可以在本地定义(在同一个剃刀文件)或全局(在 APP_ code 目录)。他们是HTML code的小片段,可以只在剃刀文件中重复使用。

Razor Helpers (@helper methods) can be defined locally (in the same razor file) or globally (in the App_Code directory). They are small snippets of HTML code that can be reused exclusively in Razor files.

最后,局部视图是可以使用 @ Html.Partial 助手包含在其他的视图文件只是普通视图文件。

And finally, Partial Views are just regular view files that can be included in other view files using the @Html.Partial helper.

我的问题是:

是否有这些功能的每一个特定的场景?或将其归结为不同的口味来达到同样的结果呢?

Is there a specific scenario for each one of these features? Or it comes down to different flavors to achieve the same result?

推荐答案

HTML助手是可重用的组件。例如的WebGrid,寻呼机等,这些分布情况如组件,并且对剃刀没有依赖性。选择此,如果:

HTML Helpers are for reusable components. e.g. WebGrid, Pager, etc. These are distributed as assemblies and have no dependency on Razor. Choose this if:


  • 功能是真正的可重复使用的,适用于任何应用程序

  • 您不希望别人修改它,希望它的版本

局部模板浏览有大的观点分成较小的部分,让事情变得更易于管理的方式。他们也为可重用性是特定于应用程序非常有用。这些是由视图引擎的位置,所以你可以在不同的地方定义的相同部分(例如查看/共享),让您每个控制器,区域或显示模式进行自定义。选择此,如果:

Partials Views are a way to split large views into smaller parts to keep things more manageable. They are also useful for reusability that is specific to your application. These are located by the view engine, so you can have the same partial defined in different places (e.g. Views/Shared), allowing you to customize per controller, area or display mode. Choose this if:


  • 功能是应用特定

  • 要每个控制器,区域或显示模式
  • 来定制

本地助手都执行相同的模板很多次,而不必重复自己的一种方式。你也可以用它来打破的意见分成​​几部分,以避免深度嵌套,但在同一个文件藏在心里。选择此,如果:

Local Helpers are a way to execute the same template many times, without having to repeat yourself. You can also use it to break views into parts to avoid deep nesting, but keeping everything in the same file. Choose this if:


  • 功能是针对特定视图

应用助手(适用于APP_ code)是当地的帮手和HTML辅助之间的混合。选择此,如果:

Application Helpers (in App_Code) are a mix between local helpers and HTML helpers. Choose this if:


  • preFER剃刀在TagBuilder

  • 请不要介意分发文件,而不是组件

  • preFER类型安全的方法调用的语法,而不是 @ Html.Partial(名称)

  • Prefer Razor over TagBuilder
  • Don't mind distributing files instead of assemblies
  • Prefer type-safe method-call syntax instead of @Html.Partial(name)

这篇关于我们什么时候应该使用HTML辅助,剃刀助手或部分景致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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