When we need to have API endpoints to return parts of our data, there is no direct support from the CMS; no easy way to get a C# model mapped to the values available in the backoffice.
You could of course create a controller inheriting from
UmbracoApi and then query your content with
As fine as this solution is, the
IPublishedContent object you get from your query doesn’t have strongly typed properties.
You have to query them one by one providing the type and name of the property (
Definitely not ideal…
That’s where we’ve come up with a solution we have packaged in a library. Using the
TypedContentAtXPath method and AutoMapper, we are able to do:
Granted you have setup AutoMapper using the provided profile:
And of course created your expected model where the properties name match those setup in the backoffice:
This is a fairly simple example but the library can do much more so make sure to check out the documentation: https://github.com/DeepFocus/uTyped.
See something wrong or want to ask a question? Get in touch on Twitter