Try our conversational search powered by Generative AI!

get html of XHTML property with nested block via ajax

Vote:
 

Hi all,

I need to get XHTML property of my block using ajax.


If I add a different block into RTE, if I call
_contentLoader.Get(id).Description.ToHtmlString(),
I get

quote
as a part of html string and if I call
_contentLoader.Get(id)?.Description?.ToInternalString()
I get "".

What is the correct way of getting full correct html (including internal block html) from RTE property in ApiController?

#151714
Aug 04, 2016 14:26
Vote:
 

Don't know if this is the correct way but I'm using this approach:

public class DummyController : Controller
{
}

public static class XhtmlStringExtensions
{
    public static string ToParsedHtmlString(this XhtmlString xhtmlstring)
    {
        if (xhtmlstring == null || xhtmlstring.IsEmpty || string.IsNullOrWhiteSpace(xhtmlstring.ToHtmlString()))
        {
            return string.Empty;
        }

        var routeData = new RouteData();
        routeData.Values.Add("controller", "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;
    }
}
#152019
Edited, Aug 12, 2016 13:43
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.