I have page that hosts an XhtmlString property and the page has multiple translations. In the XhtmlString area there is also a block, which has multiple translations as well. I am trying to get the XhtmlString in a WebApi context and convert it to raw HTML. StaticFragment items in XhtmlString is being read correctly from the database according the language parameter I send to IContentLoader.Get<>. Example:
var page = _contentLoader.Get<OccupationInfoPage>(page.ContentLink, new CultureInfo("en");
I am than using the following method to convert the XhtmlString to raw HTML:
public static string ToRawHtml(this XhtmlString xhtmlstring)
{
if (string.IsNullOrWhiteSpace(xhtmlstring?.ToHtmlString()) || xhtmlstring.IsEmpty)
{
return string.Empty;
}
var routeData = new RouteData();
routeData.Values.Add("controller", "a");
routeData.Values.Add("subject", "a");
var hh = new HtmlHelper(new ViewContext()
{
HttpContext = new HttpContextWrapper(HttpContext.Current),
ViewData = new ViewDataDictionary(),
TempData = new TempDataDictionary(),
Controller = new DummyController(),
RouteData = routeData
}, new ViewPage());
string result;
using (var writer = new StringWriter())
{
hh.ViewContext.Writer = writer;
hh.RenderXhtmlString(xhtmlstring);
writer.Flush();
result = writer.ToString();
}
return result;
}
While this method returns the raw HTML I am looking for, with normalized links, it also defaults to master language for ContentFragment items. Is there any way to set the language in HtmlHelper object or somewhere else so I can get the correct version, both for the blocks and the links?