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;
}
}
}
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:
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?
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.
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:
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
is working on a reply...