protected void UpdateTemplateSections(string requestUri, Response response, WebTemplatePage content, WebUrl url)
        {
            foreach (WebSection section in content.Data.Sections)
            {
                var sectionJson = (SectionPage)content.Sections[section.Name];

                var uri = section.GetMappingUrl(url);
                sectionJson.PinContent = Self.GET(uri);

                var json = response.Resource as Json;
                if (section.Default && json != null && sectionJson.MainContent?.RequestUri != requestUri)
                {
                    if (json is WrapperPage)
                    {
                        //we are inserting WebsiteProvider to WebsiteProvider
                        sectionJson.MainContent = json as WrapperPage;
                    }
                    else
                    {
                        //we are inserting different app to WebsiteProvider
                        var page = sectionJson.MainContent;
                        if (page == null || page.WebTemplatePage.Data != null)
                        {
                            page = GetContainerPage(requestUri);
                            sectionJson.MainContent = page;
                        }

                        page.RequestUri = requestUri;
                        page.Reset();
                        page.MergeJson(json);
                    }
                }
            }
        }
        protected void InitializeTemplate(WebTemplatePage content)
        {
            dynamic namedSections = new Json();

            content.Sections = namedSections;

            foreach (WebSection section in content.Data.Sections)
            {
                SectionPage sectionJson = new SectionPage();
                namedSections[section.Name] = sectionJson;
                sectionJson.Name            = section.Name;
            }
        }
        public void Register()
        {
            Application.Current.Use(new HtmlFromJsonProvider());
            Application.Current.Use(new PartialToStandaloneHtmlProvider());

            Handle.GET("/WebsiteProvider", () =>
            {
                return("Welcome to WebsiteProvider.");
            });

            Handle.GET("/WebsiteProvider/partial/template/{?}", (string templateId) =>
            {
                var page = new WebTemplatePage
                {
                    Data = GetWebTemplate(templateId)
                };
                InitializeTemplate(page);
                return(page);
            });

            Handle.GET("/WebsiteProvider/partial/layout/{?}", (string templateId) =>
            {
                WrapperPage page;

                if (Session.Current != null)
                {
                    page = Session.Current.Data as WrapperPage;
                    var sessionWebTemplate = page?.WebTemplatePage.Data;

                    if (sessionWebTemplate != null)
                    {
                        var webTemplate = GetWebTemplate(templateId);
                        if (sessionWebTemplate.Equals(webTemplate))
                        {
                            return(page);
                        }
                    }
                }
                else
                {
                    Session.Current = new Session(SessionOptions.PatchVersioning);
                }

                page = new WrapperPage
                {
                    Session = Session.Current
                };

                if (page.Session.PublicViewModel != page)
                {
                    page.Session.PublicViewModel = page;
                }

                return(page);
            });

            Handle.GET("/WebsiteProvider/partial/wrapper?uri={?}&response={?}", (string requestUri, string responseKey) =>
            {
                Response currentResponse = ResponseStorage.Get(responseKey);
                WebUrl webUrl            = this.GetWebUrl(requestUri);
                WebTemplate template     = webUrl?.Template;

                if (template == null)
                {
                    throw new Exception("Default template is missing");
                }

                WrapperPage master = GetLayoutPage(template);
                master.IsFinal     = webUrl.IsFinal || string.IsNullOrEmpty(webUrl.Url);

                if (!template.Equals(master.WebTemplatePage.Data))
                {
                    master.WebTemplatePage = GetTemplatePage(template.GetObjectID());
                }

                UpdateTemplateSections(requestUri, currentResponse, master.WebTemplatePage, webUrl);

                return(master);
            });

            RegisterFilter();
        }