private HttpResponseMessage GetMacroResultAsHtml(string macroAlias, int pageId, IDictionary <string, object> macroParams) { // note - here we should be using the cache, provided that the preview content is in the cache... var doc = Services.ContentService.GetById(pageId); if (doc == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } //need to get a legacy macro object - eventually we'll have a new format but nto yet var macro = new macro(macroAlias); if (macro == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } //if it isn't supposed to be rendered in the editor then return an empty string if (macro.DontRenderInEditor) { var response = Request.CreateResponse(); //need to create a specific content result formatted as html since this controller has been configured //with only json formatters. response.Content = new StringContent(string.Empty, Encoding.UTF8, "text/html"); return(response); } //because macro's are filled with insane legacy bits and pieces we need all sorts of wierdness to make them render. //the 'easiest' way might be to create an IPublishedContent manually and populate the legacy 'page' object with that //and then set the legacy parameters. var legacyPage = new global::umbraco.page(doc); UmbracoContext.HttpContext.Items["pageID"] = doc.Id; UmbracoContext.HttpContext.Items["pageElements"] = legacyPage.Elements; UmbracoContext.HttpContext.Items[global::Umbraco.Core.Constants.Conventions.Url.AltTemplate] = null; var renderer = new UmbracoComponentRenderer(UmbracoContext); var result = Request.CreateResponse(); //need to create a specific content result formatted as html since this controller has been configured //with only json formatters. result.Content = new StringContent( renderer.RenderMacro(macro, macroParams, legacyPage).ToString(), Encoding.UTF8, "text/html"); return(result); }
private HttpResponseMessage GetMacroResultAsHtml(string macroAlias, int pageId, IDictionary <string, object> macroParams) { // note - here we should be using the cache, provided that the preview content is in the cache... var doc = Services.ContentService.GetById(pageId); if (doc == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } var m = Services.MacroService.GetByAlias(macroAlias); if (m == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } var macro = new MacroModel(m); //if it isn't supposed to be rendered in the editor then return an empty string if (macro.RenderInEditor == false) { var response = Request.CreateResponse(); //need to create a specific content result formatted as html since this controller has been configured //with only json formatters. response.Content = new StringContent(string.Empty, Encoding.UTF8, "text/html"); return(response); } //because macro's are filled with insane legacy bits and pieces we need all sorts of wierdness to make them render. //the 'easiest' way might be to create an IPublishedContent manually and populate the legacy 'page' object with that //and then set the legacy parameters. // When rendering the macro in the backoffice the default setting would be to use the Culture of the logged in user. // Since a Macro might contain thing thats related to the culture of the "IPublishedContent" (ie Dictionary keys) we want // to set the current culture to the culture related to the content item. This is hacky but it works. var publishedContent = UmbracoContext.ContentCache.GetById(doc.Id); var culture = publishedContent?.GetCulture(); if (culture != null) { Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(culture.Culture); } var legacyPage = new global::umbraco.page(doc, _variationContextAccessor); UmbracoContext.HttpContext.Items["pageElements"] = legacyPage.Elements; UmbracoContext.HttpContext.Items[Core.Constants.Conventions.Url.AltTemplate] = null; var renderer = new UmbracoComponentRenderer(UmbracoContext); var result = Request.CreateResponse(); //need to create a specific content result formatted as html since this controller has been configured //with only json formatters. result.Content = new StringContent( renderer.RenderMacro(macro, macroParams, legacyPage).ToString(), Encoding.UTF8, "text/html"); return(result); }