Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Giovanni Sidoel 94 posts 233 karma points
    Aug 03, 2015 @ 19:35
    Giovanni Sidoel
    0

    DynamicNode or iPublishedContent as JSON

    Hey guys,

    I'm writing a webAPI in which I would like to return page data (all of its properties and it's values) as a JSON string. I Just keep getting back empty. Anybody have any idea how to do this?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using umbraco.MacroEngines;
    using Umbraco.Web.WebApi;
    using Newtonsoft;
    using Newtonsoft.Json;
    using System.Web.Mvc;
    using Umbraco.Web;
    using Umbraco.Core.Models;
    
    namespace SoengNgie.Controllers
    {
        public class ProductenController : UmbracoApiController
        {
            [AcceptVerbs("POST", "OPTIONS")]
            public string getMerken()
            {
                string thePayload = "";
    
                var umbracoHelper = new Umbraco.Web.UmbracoHelper(UmbracoContext.Current);
    
                dynamic root = new DynamicNode(-1);
                dynamic home = root.DescendantsOrSelf("Home").First();
    
                dynamic productenOverzicht = home.Children().Where("NodeTypeAlias == \"ProductenOverzicht\"").First();
    
                dynamic merken = productenOverzicht.Children();
    
                List<IPublishedContent> l = new List<IPublishedContent>();
    
                foreach (var merk in merken)
                {
                    IPublishedContent page = umbracoHelper.Content(merk.Id);
                   // l.Add(page);
                    thePayload += JsonConvert.SerializeObject(page);
                    //var anon = new();
                    //var properties = merk.getProperties();
                   // foreach (var property in properties)
                    //{
    
                    //}
                }
    
               // thePayload = JsonConvert.SerializeObject(l);
    
    
                return thePayload;
            }
        }
    }
    
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 05, 2015 @ 07:50
    Marc Goodson
    0

    One thing when using WebApi, is you have to make sure your Post/Get Method attribute comes from the System.Web.Http namespace(rather than System.Web.Mvc as would be the case in MVC controllers)

    eg.

    [System.Web.Http.HttpGet]
        public string GetUmbracoStuff()
    

    Another thing, the WebApi is clever enough to return content in the appropriate format for the request, (eg serverside xml or a client side request in Json) - but this relies on the objects you are returning being 'serializable'. (so dynamic objects would present a problem)

    What I tend to do is create a simple (POCO) class with the properties I need to return in the API, and then in the WebApi Action, populate these plain classes and build up a list to return that allows the WebApi to serialize to XML or Json depending on the request.

    You can force the controller to always return Json, even for a serverside request by removing the XML Formatter from the Controller:

       protected override void Initialize(global::System.Web.Http.Controllers.HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            controllerContext.Configuration.Formatters.Remove(controllerContext.Configuration.Formatters.XmlFormatter);
        }
    
  • Alex Skrypnyk 6162 posts 24132 karma points MVP 8x admin c-trib
    Aug 05, 2015 @ 13:18
    Alex Skrypnyk
    0

    Hi Goivanni,

    Our advice will be - never use dynamic ))

    Of course it's just our opinion, but dynamic is slow and hard to debug, so we don't use it at our umbraco projects at all.

    If you want to return some data from API best way will be use view model classes. Not dynamic and Not IPublishedContent.

    Thanks, Alex

Please Sign in or register to post replies

Write your reply to:

Draft