Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Philip Hayton 98 posts 435 karma points
    May 11, 2019 @ 08:40
    Philip Hayton
    1

    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.

  • Marc Goodson 2126 posts 14218 karma points MVP 8x c-trib
    May 11, 2019 @ 13:35
    Marc Goodson
    1

    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

  • Philip Hayton 98 posts 435 karma points
    May 12, 2019 @ 06:54
    Philip Hayton
    0

    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

  • Marc Goodson 2126 posts 14218 karma points MVP 8x c-trib
    May 12, 2019 @ 07:15
    Marc Goodson
    102

    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

  • Philip Hayton 98 posts 435 karma points
    May 12, 2019 @ 07:26
    Philip Hayton
    0

    Ah that's brilliant. This'll work perfectly.

    Thanks so much for your assistance Marc. Much appreciated!

    Kind regards

    Phil

Please Sign in or register to post replies

Write your reply to:

Draft