我们应该在什么时候使用Html helper, Razor helper还是部分视图?

时间:2023-02-01 20:02:55

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:

这三个不同的特性存在于Razor视图引擎中,可以用来实现类似的结果。最后,它们三个都只是呈现HTML代码片段,但是定义和使用它们的方式是完全不同的。我知道:

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.

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

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.

Razor helper (@helper方法)可以在本地(在相同的Razor文件中)或全局定义(在App_Code目录中)。它们是HTML代码的小片段,可以在Razor文件中重用。

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

最后,部分视图只是常规的视图文件,可以使用@Html包含在其他视图文件中。部分辅助。

My question is:

我的问题是:

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

每个特性都有特定的场景吗?还是因为不同的口味而得到相同的结果?

1 个解决方案

#1


60  

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:

HTML helper是可重用组件的。例如,WebGrid、寻呼机等。它们作为组件分布,不依赖于Razor。选择这个如果:

  • Functionality is truly reusable and applicable to any application
  • 功能是真正可重用的,并且适用于任何应用程序
  • You don't want people to modify it, want to version it
  • 你不希望人们修改它,想要版本化它

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:

局部视图是一种将大视图分割成更小的部分的方法,以使事情更易于管理。它们对于特定于应用程序的可重用性也很有用。它们是由视图引擎定位的,因此您可以在不同的位置(例如,view /Shared)定义相同的部分,允许您根据控制器、区域或显示模式进行自定义。选择这个如果:

  • Functionality is application-specific
  • 功能是特定于应用程序的
  • Want to customize per controller, area or display mode
  • 想要自定义每个控制器,区域或显示模式。

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:

本地helper是一种多次执行相同模板的方法,无需重复。您还可以使用它将视图分解成多个部分,以避免深度嵌套,但要将所有内容保存在同一个文件中。选择这个如果:

  • Functionality is view-specific
  • 功能视图相关

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

Application helper(在App_Code中)是本地helper和HTML helper的混合体。选择这个如果:

  • Prefer Razor over TagBuilder
  • 喜欢剃须刀在TagBuilder
  • Don't mind distributing files instead of assemblies
  • 不要介意分发文件而不是程序集
  • Prefer type-safe method-call syntax instead of @Html.Partial(name)
  • 更喜欢类型安全的方法调用语法而不是@Html.Partial(名称)

#1


60  

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:

HTML helper是可重用组件的。例如,WebGrid、寻呼机等。它们作为组件分布,不依赖于Razor。选择这个如果:

  • Functionality is truly reusable and applicable to any application
  • 功能是真正可重用的,并且适用于任何应用程序
  • You don't want people to modify it, want to version it
  • 你不希望人们修改它,想要版本化它

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:

局部视图是一种将大视图分割成更小的部分的方法,以使事情更易于管理。它们对于特定于应用程序的可重用性也很有用。它们是由视图引擎定位的,因此您可以在不同的位置(例如,view /Shared)定义相同的部分,允许您根据控制器、区域或显示模式进行自定义。选择这个如果:

  • Functionality is application-specific
  • 功能是特定于应用程序的
  • Want to customize per controller, area or display mode
  • 想要自定义每个控制器,区域或显示模式。

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:

本地helper是一种多次执行相同模板的方法,无需重复。您还可以使用它将视图分解成多个部分,以避免深度嵌套,但要将所有内容保存在同一个文件中。选择这个如果:

  • Functionality is view-specific
  • 功能视图相关

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

Application helper(在App_Code中)是本地helper和HTML helper的混合体。选择这个如果:

  • Prefer Razor over TagBuilder
  • 喜欢剃须刀在TagBuilder
  • Don't mind distributing files instead of assemblies
  • 不要介意分发文件而不是程序集
  • Prefer type-safe method-call syntax instead of @Html.Partial(name)
  • 更喜欢类型安全的方法调用语法而不是@Html.Partial(名称)