I am trying to use v8 as a sort of headless CMS. In a nutshell, most of the routes that would normally return server side generated HTML, I would prefer instead to return JSON.
I have been using a custom UmbracoApiController to handle this, by passing in a ?url= parameter and passing that to ContentCache.GetByRoute().
This works as intended for the most part (although culture variants do not seem to work using this approach), but it's not the most graceful. So I wanted to investigate simply replacing the Default Umbraco Controller to hijack all routes.
I began using this example, but the interfaces requires you to implement your own Execute method. I'm assuming some important pipeline steps are supposed to take place inside Execute but I can't find examples anywhere.
Can anybody shed any light?
Also if anyone has any thoughts / opinions on my approach I'd love to hear them. I tend to operate in a vaccum at times so welcome other perspectives.
If you make your new controller inherit from the base RenderMvcController then you won't have to implement Execute .. only override the Index action (which will have passed into it the ContentModel representing the content which Umbraco would return for the request) from this you can construct your JSON blob and return...
... On that documentation page look at the HomeController example under 'Creating a custom controller's (but call your new default controller something meaningful) and instead of returning base Index or Current template... Return Json(yourJsonObject)
If I went with that approach would I not need to create a new controller for every doc type? Ideally I'd like to avoid this, although if push comes to shove it may have to be done.
Set your new controller as the DefaultRenderMvcController for the entire site...
'the routing by doc type alias is a convention based on names of controller's you will be telling Umbraco to use your single controller .by default for everything...after doing the above 'you could' in a niche circumstance override your new default implementation for a particular doc type by creating a new MVC controller following the matching alias naming convention...
Concrete example of Custom Default Controller
Hi guys,
I am trying to use v8 as a sort of headless CMS. In a nutshell, most of the routes that would normally return server side generated HTML, I would prefer instead to return JSON.
I have been using a custom UmbracoApiController to handle this, by passing in a ?url= parameter and passing that to ContentCache.GetByRoute().
This works as intended for the most part (although culture variants do not seem to work using this approach), but it's not the most graceful. So I wanted to investigate simply replacing the Default Umbraco Controller to hijack all routes.
I began using this example, but the interfaces requires you to implement your own Execute method. I'm assuming some important pipeline steps are supposed to take place inside Execute but I can't find examples anywhere.
Can anybody shed any light?
Also if anyone has any thoughts / opinions on my approach I'd love to hear them. I tend to operate in a vaccum at times so welcome other perspectives.
Hi Phillip
If you make your new controller inherit from the base RenderMvcController then you won't have to implement Execute .. only override the Index action (which will have passed into it the ContentModel representing the content which Umbraco would return for the request) from this you can construct your JSON blob and return...
... On that documentation page look at the HomeController example under 'Creating a custom controller's (but call your new default controller something meaningful) and instead of returning base Index or Current template... Return Json(yourJsonObject)
Regards
Marc
Hi Marc,
Thanks for the reply.
If I went with that approach would I not need to create a new controller for every doc type? Ideally I'd like to avoid this, although if push comes to shove it may have to be done.
Kind regards
Phil
Hi Phil
No, just create one MVC controller ..
Call it
PhilsDefaultRenderMvcController
Make it inherit from RenderMvcController
Override index action with your implementation
Set your new controller as the DefaultRenderMvcController for the entire site...
'the routing by doc type alias is a convention based on names of controller's you will be telling Umbraco to use your single controller .by default for everything...after doing the above 'you could' in a niche circumstance override your new default implementation for a particular doc type by creating a new MVC controller following the matching alias naming convention...
Regards
Marc
Ah that's brilliant. This'll work perfectly.
Thanks so much for your assistance Marc. Much appreciated!
Kind regards
Phil
is working on a reply...