Sitecore View Rendering and Controller Rendering Helper

做~自己de王妃 提交于 2020-01-02 02:46:08

问题


I have a sitecore application that use this method :

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

This works perfectly fine even without I create an item for that rendering in Sitecore CMS

Then what is the difference between that method with simple ASP MVC method :

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

Both same or not? I feel little confused here


回答1:


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

will trigger mvc.renderRendering pipeline. And your view will be rendered in almost a same way if you add it to placeholder. Difference from Html.Partial is in cycle of processing your view. Rendered result could be different if you depend on something in that pipeline (e.g. Caching as mentioned @Gatogordo). (or if you added some processor there by yourself). If you want you rendering be the same if you add them via placeholder then use Html.Sitecore().ViewRendering

For

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

and

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

difference also is in it's execution lifecycle. Sitecore ones is executed via ControllerRunner that gets Controller from SitecoreControllerFactory and execute some action. ASP.Net MVC action is executed via HttpContext.Server.Execute and do actually the same. But looking on implementation I can make an assumption one of differences is in routing. In case of using ASP.Net MVC helper your route values can bring you to some Sitecore item rather that required controller action if it will match. Sitecore helper will always execute controller.

If you need more details you can open System.Web.Mvc.Html.ChildActionExtensions.Action and Sitecore.Mvc.Helpers.SitecoreHelper.Controller in reflector and compare them step by step.




回答2:


Both are similar but not exactly the same. The ones from the Sitecore helper will add a little Sitecore flavor to the common mvc ones (for example adding caching possibilities) but will also have a (small) performance hit.

It's a choice that needs to be made, and it depends on your rendering and the context..



来源:https://stackoverflow.com/questions/36996267/sitecore-view-rendering-and-controller-rendering-helper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!