Sitecore View Rendering 和 Controller Rendering Helper

Sitecore View Rendering and Controller Rendering Helper

我有一个使用这种方法的 sitecore 应用程序:

@Html.Sitecore().ViewRendering("Path to the View")
@Html.Sitecore().Controller("Controller Name", "Controller Action")

即使我没有在 Sitecore CMS 中为该呈现创建项目,这也能正常工作

那么该方法与简单 ASP MVC 方法有什么区别:

@Html.Partial("Path to the View")
@Html.Action("Controller Name", "Controller Action")

两者是否相同?我在这里感到有点困惑

两者相似但不完全相同。来自 Sitecore 帮助程序的那些将为常见的 mvc 增加一点 Sitecore 风味(例如添加缓存可能性),但也会有(小的)性能影响。

这是一个需要做出的选择,它取决于你的渲染和上下文..

@Html.Sitecore().ViewRendering("Path to the View") 

将触发 mvc.renderRendering 管道。如果您将视图添加到占位符,您的视图将以几乎相同的方式呈现。与 Html.Partial 的区别在于处理您的视图的周期。如果您依赖于该管道中的某些东西(例如 @Gatogordo 提到的缓存),则渲染结果可能会有所不同。 (或者如果您自己在那里添加了一些处理器)。 如果您希望通过占位符添加它们时渲染相同,请使用 Html.Sitecore().ViewRendering

为了

@Html.Sitecore().Controller("Controller Name", "Controller Action")

@Html.Action("Controller Name", "Controller Action")

不同之处还在于它的执行生命周期。 Sitecore 是通过 ControllerRunner 执行的,它从 SitecoreControllerFactory 获取 Controller 并执行一些操作。 ASP.Net MVC 操作通过 HttpContext.Server.Execute 执行,实际上是一样的。但是从实现的角度来看,我可以假设差异之一在于路由。在使用 ASP.Net MVC 助手的情况下,您的路由值可以将您带到某个 Sitecore 项目,而不是如果它匹配则需要控制器操作。 Sitecore 助手将始终执行控制器。

如果您需要更多详细信息,可以在反射器中打开 System.Web.Mvc.Html.ChildActionExtensions.Action 和 Sitecore.Mvc.Helpers.SitecoreHelper.Controller 并逐步比较它们。