Copied to clipboard

Flag this post as spam?

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


  • Abilash Ashok 63 posts 418 karma points
    Nov 13, 2018 @ 08:49
    Abilash Ashok
    0

    REST implementation in Umbraco

    Hello,

    I want to use Umbraco for one of my project backend to manage the contents. Besides just having the contents (webpages, images, docs, etc.), the website also has APIs also so that users can access contents through apis.

    Recently, when I checked Umbraco (after a break), I saw that REST APIs are discontinued for Umbraco Headless.

    Here is what I would like to know:

    1. Is it possible to create APIs in Umbraco that has endpoint - /api/your_controller/method_name instead of /umbraco/surface/your_controller/method_name?

    2. Has anyone used APIs in Umbraco in format /api/your_controller/method_name? How was your experience? Is that like a cake-walk?

    3. Can someone please suggest which is best approach to take here? Should I rely of REST APIs (which is outside support cycle) or should choose Headless for api implementation.

    Thanks in advance.

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Nov 13, 2018 @ 09:05
    Nik
    0

    Hi Abilash,

    It depends what you are trying to do. If you are still wanting to serve a full website as part of your Umbraco instance the Umbraco Headless offering might not be quite what you are after.

    There is a new package/git repo that does something similar to what you are after so it might be worth having a look at it here: https://github.com/mattbrailsford/umbraco-headrest

    Additionally, there is a GraphQL experimental git repo here: https://github.com/rasmusjp/umbraco-graphql (I know this isn't a REST implementation but thought I'd mention it)

    Nik

  • Bharani Dharan Jayasuri 12 posts 126 karma points c-trib
    Nov 13, 2018 @ 09:05
    Bharani Dharan Jayasuri
    0

    Hi Abilash,

    I have not had the opportunity to use Umbraco Rest APIs myself, but for the projects I have worked on so far, I used to write my own web API Controllers and action implementations to expose just the data I want to expose using the right Service (ContentService, MediaService etc)

    https://our.umbraco.com/documentation/reference/routing/webapi/

  • Abilash Ashok 63 posts 418 karma points
    Nov 13, 2018 @ 09:13
    Abilash Ashok
    0

    Hello Bharani Dharan Jayasuri,

    Thanks for the reply. By default, apis we create in Umbraco has format /umbraco/api/controller/method or /umbraco/surface/controller/method.

    I am looking for apis in Umbraco whose route looks like /api/controller/method. Is it possible to get that route which is more generic and common, instead of having /umbraco sticking to the api endpoint?

    Is that something your were able to achieve when you developed APIs in Umbraco?

  • Bharani Dharan Jayasuri 12 posts 126 karma points c-trib
    Nov 13, 2018 @ 11:32
    Bharani Dharan Jayasuri
    100

    Abilash,

    You should be able to register custom Api routes like this,

    public class EventHandler : IApplicationEventHandler
        {
            public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                //throw new NotImplementedException();
            }
    
            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                //throw new NotImplementedException();
                WebApiConfig.Register(GlobalConfiguration.Configuration);
    
            }
    
            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                //throw new NotImplementedException();
            }
        }
    
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                config.Routes.MapHttpRoute(
                    name:"CustomApi",
                    routeTemplate:"api/{controller}/{action}",
                    defaults: new {controller = "CustomApiRoute", action = "GetSomeString" }
                    );
            }
        }
    

    I just checked it locally and I could hit a sample get action of an UmbracoApiController I wrote like this,

    http://localhost:7130/api/customapiroute/getsomestring

  • Abilash Ashok 63 posts 418 karma points
    Nov 13, 2018 @ 11:35
    Abilash Ashok
    0

    That's great to hear. I'll give a spin.

    Thanks for the help :)

Please Sign in or register to post replies

Write your reply to:

Draft