The Umbraco Content Api is a package that enables easy integration of Headless Api functionality into your project. The package includes converters for all default Umbraco porperty editors and allows developers to add to and replace them at will.
Out of the box easy to use, full DI support and fast.
- Download the package from NuGet
- Install the package
- Create an UmbracoApiController
- Inject the content resolver
- Resolve the content
public class SampleApiController : UmbracoApiController
{
private readonly Lazy<IContentResolver> _contentResolver;
public ContentApiController(Lazy<IContentResolver> contentResolver)
{
_contentResolver = contentResolver;
}
public IHttpActionResult Get(Guid id)
{
IPublishedContent content = Umbraco.Content(id);
var model = _contentResolver.Value.ResolveContent(content);
return Ok(model);
}
}
To create a converter for your custom editor you need to implement the IConverter
interface.
// Converter:
public class SampleConverter : IConverter
{
public string EditorAlias => "My.PropertyEditorAlias";
public object Convert(object value)
{
// If the value is already in a json supported format, just return it.
// Otherwise convert it to a friendly format here.
return value;
}
}
// Composer:
[ComposeAfter(typeof(UmbracoContentApi.Core.Composers.ConvertersComposer))]
public class ConverterComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Converters().Append<SampleConverter>();
}
}
To replace a converter just add the following to the composer:
composition.Converters()
.Replace<ConverterToReplace, SampleConverter>();
The Umbraco Content Api works with Umbraco 8.1.3+