Copied to clipboard

Flag this post as spam?

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


  • Biagio Paruolo 1472 posts 1611 karma points c-trib
    Jun 21, 2019 @ 13:23
    Biagio Paruolo
    0

    How to overloading or change name to /umbraco/api/ route?

    Is't possibile change the name or add an alternative URL routing for Umbraco API Controller ( controller with UmbracoApiController )? That is: Normal: /umbraco/api/

    Thanks

  • Marc Goodson 995 posts 6568 karma points MVP 4x c-trib
    Jun 23, 2019 @ 09:34
    Marc Goodson
    0

    Hi Biagio

    I think if I understand correctly you might want to use Web API attribute routes to change the urls for your api endpoint?

    Something like this?

    using System.Collections.Generic;
    using System.Web.Http;
    using Umbraco.Web.WebApi;
    
    namespace Umbraco8.Controllers
    {
        public class MyCustomApiController : UmbracoApiController
        {
            [HttpGet]
            [Route("umbraco/api/things")]
            public IEnumerable<string> GetThings)
            {
                return new[] { "Table", "Chair", "Desk", "Computer", "Beer fridge" };
            }     
        }
    }
    

    which would make the url:

    /umbraco/api/things to return the list provided by GetThings endpoint

    For this to work you'd need to call MapHttpAttributeRoutes method as Umbraco starts up, if you add a class that inherits IComponent you can call this method on Initialize() and add another class inheriting IComposer to run the Component at start-up eg:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Umbraco.Core.Composing;
    using Umbraco.Web;
    using Umbraco.Web.Mvc;
    
    namespace Umbraco8.Components
    {
    
         [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class RegisterCustomApiRoutesComposer : IComposer
        {
     composition.Components().Insert<RegisterCustomApiRoutesComponent>();
        }
    
        public class RegisterCustomApiRoutesComponent : IComponent
        {
    
    
            public void Initialize()
            {
                GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
    }
    
            public void Terminate()
            {
                throw new NotImplementedException();
            }
        }
    }
    

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft