2

Does any one has experience implementing hybrid version of Sitecore MVC and webforms?

Sitecore does support the hybrid state but at a page level, meaning either the page has to be on Sitecore MVC layout or ASPX layout, but my requirement is to have a MVC rendering on ASPX layout.

Tried to implement this approach which is available on web, it does help render the MVC rendering on the page but as soon as a controller action is invoked the partial view rendering shows up without the parent layout as shown in the below snapshot. enter image description here Due to the above issue, we have decided to have a full page based on MVC layout but it would be good to know if anyone has any experience of such thing.

Regards, Pratik Thakker

2
  • 1
    There is also a post here by Hedgehog where they explain their approach to WebForms / MVC interop. The accompanying repo can be found here. I've had this setup in a 7.5 solution and it's fairly painless to integrate, just drop in the WrapMvcRendering.cs pipeline and add the patch file. Hope this helps. Mar 31, 2017 at 8:18
  • Thanks @Squaretechre, The article that you pointed me towards is the same that I have mentioned in my question. I am using Sitecore 8.2 and maybe that approach no longer works with sitecore 8.x
    – PratikT
    Apr 11, 2017 at 4:42

1 Answer 1

2

Have a read of this blog post that explains how to use MVC layouts on Webforms pages: https://blog.horizontalintegration.com/2015/09/15/mixing-sublayout-with-rendering-webforms-with-mvc-request/. It references a couple of examples such as the one you mentioned.

The solution consists of a Extension Method for the Sublayout:

public static class SubLayoutExtension
    {
        public static string RenderSublayoutHTML(this Sublayout subLayout)
        {
            string retHTML = string.Empty;

            var page = new DummyPage();

            if (HttpContext.Current.Items.Contains("ismvc"))
            {
                var userControl = page.LoadControl(subLayout.Path);
                subLayout.Controls.Add(userControl);

            }
            page.Controls.Add(subLayout);
            retHTML = ExecutePage(page);


            return retHTML;
        }
        private static string ExecutePage(DummyPage page)
        {
            page.ProcessRequest(HttpContext.Current);
            return page.Result;
        }
    }  

You can then call it like so:

var item = e.Item.DataItem as Item;
                if (item == null) return;

                var slideImage = e.Item.FindControl("slideImage") as Literal;
                if (slideImage == null) return;

                Sublayout slideSublayout = new Sublayout();
                slideSublayout.DataSource = item.Paths.FullPath;

                if (item.TemplateID.Equals(new ID(ImageSlideItem.TemplateId)))
                {
                    slideSublayout.Path = "~/Common/Sublayouts/PageElements/Image/Image.ascx";
                }
                else if (item.TemplateID.Equals(new ID(VideoSlideItem.TemplateId)))
                {
                    slideSublayout.Path = "~/Common/Sublayouts/PageElements/Video/HostedVideo.ascx";
                }
                else if (item.TemplateID.Equals(new ID(YouTubeVideoSlideItem.TemplateId)))
                {
                    slideSublayout.Path = "~/Common/Sublayouts/PageElements/Video/YouTubeVideo.ascx";
                }
                slideImage.Text = slideSublayout.RenderSublayoutHTML();

As referenced in the article this is another way of doing it, using MVC pages and views and rendering user controls within them instead: http://bisstalk.blogspot.co.uk/2014/10/sitecore-sublayouts-rendering-on.html

Hopefully this is useful.

1
  • Thanks Adam for your answer. I have got pulled into other business critical tasks so am not able to investigate further on the solution provided by you. Would get to be in a few days and post my feedback here.
    – PratikT
    Apr 11, 2017 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.