I can't seem to find any documentation helping me in understanding how to get strongly typed content inside my own custom api controller.
This is what i have for now:
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Cms.Web.Common;
using Umbraco.Cms.Web.Common.PublishedModels;
namespace universe_web.Custom_Controllers.Api
{
public class ExperiencesController : UmbracoApiController
{
private readonly UmbracoHelper _umbracoHelper;
public ExperiencesController(UmbracoHelper umbracoHelper)
=> _umbracoHelper = umbracoHelper;
[HttpGet]
public Oplevelse[] GetAll()
{
var selection = _umbracoHelper.Content(Guid.Parse("777c01de-34ab-4630-a2e7-e8bf2b40b35e")).ChildrenOfType("oplevelse");
return selection;
}
}
}
As you can see, i would like to return an array of strongly typed "Oplevelse" objects (auto converted to json by .net). However i can't seem to find out how i can turn the .Content query into a list of "Oplevelse" objects instead of the generic IPublishedContent.
Yes that is my problem, I can't seem to figure out how to properly cast this to an array of "Oplevelse".
A different approach would be to not use the auto generated classes and make an "Oplevelse" class myself and then manually mapping these objects. This would also enable me to use the in-memory model builder option instead but would involve more work creating the classes.
However it won't work as the generated types from the model builder cannot be converted to json objects:
NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported.
System.Text.Json.Serialization.Converters.UnsupportedTypeConverter<T>.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. Path: $.Ekstra.ContentType.PropertyTypes.ModelClrType.
System.Text.Json.ThrowHelper.ThrowNotSupportedException(ref WriteStack state, NotSupportedException ex)
So it seems the generated classes can't directly be used to create an API.
I also can't find any documentation on API creation. All i can find is that Umbraco decided to stop support for their own rest api and a very sparse documentation on creating an api controller but not on how to then actually publish data.
That makes complete sense, you may have to deal with two different cases here then. Get the data from Umbraco as you currently have, then map that data to a new class that can be used as the json response. Probably better from SRP outlook too.
I ended up taking the route of creating my own objects and mapping which has the benefit that i can again set the modelbuilder to be in-memory and also, as you describe, save a lot on the json data as i can map only the properties that i need!
Getting strongly typed content inside controller
Hi,
I can't seem to find any documentation helping me in understanding how to get strongly typed content inside my own custom api controller.
This is what i have for now:
As you can see, i would like to return an array of strongly typed "Oplevelse" objects (auto converted to json by .net). However i can't seem to find out how i can turn the .Content query into a list of "Oplevelse" objects instead of the generic IPublishedContent.
Have you tried to cast the IPublishedContent to a models builder model?
Hi,
Yes that is my problem, I can't seem to figure out how to properly cast this to an array of "Oplevelse".
A different approach would be to not use the auto generated classes and make an "Oplevelse" class myself and then manually mapping these objects. This would also enable me to use the in-memory model builder option instead but would involve more work creating the classes.
You could add an 'as IEnumerable< T > ' to the end, or try an expression such as what is described here:
https://our.umbraco.com/forum/extending-umbraco-and-using-the-api/96571-model-builder-how-to-covert-ipublishedcontent-in-to-strongly-typed-model
There are a few others that can be used to cast also, but typically an 'as T' works well
Thanks Conor!
The .OfType<> did the trick!
However it won't work as the generated types from the model builder cannot be converted to json objects:
So it seems the generated classes can't directly be used to create an API.
I also can't find any documentation on API creation. All i can find is that Umbraco decided to stop support for their own rest api and a very sparse documentation on creating an api controller but not on how to then actually publish data.
Is there any documentation on this anywhere?
That makes complete sense, you may have to deal with two different cases here then. Get the data from Umbraco as you currently have, then map that data to a new class that can be used as the json response. Probably better from SRP outlook too.
I can see they have this documentation: https://our.umbraco.com/documentation/Reference/Routing/Umbraco-API-Controllers/
Not sure if this is the one you are referencing?
Yes that is the doc. i'm referencing :)
I ended up taking the route of creating my own objects and mapping which has the benefit that i can again set the modelbuilder to be in-memory and also, as you describe, save a lot on the json data as i can map only the properties that i need!
Thanks for all the inputs and help.
No problem - glad its helped 😊
is working on a reply...