Copied to clipboard

Flag this post as spam?

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


  • Nathan Reece 62 posts 376 karma points
    Mar 31, 2021 @ 10:02
    Nathan Reece
    0

    Umbraco 8 Changing the default route of UmbracoApiController

    I am working with UmbracoApiController and I want to change the default path to be something more friendly and more importantly not contain the "umbraco/api" stuff.

    so my controller is call

    public class ResidenceApiController : _BaseWebApiController {

    }

    I want to make the path to the API something like this....

    /residence/api/getall

    Does anyone know if this is possible?

    Thanks in advance

  • David Armitage 510 posts 2082 karma points
    Mar 31, 2021 @ 10:48
    David Armitage
    2

    Hi Nathan,

    Yes this is possible. There is a good tutorial here. https://our.umbraco.com/documentation/reference/routing/webapi/

    Look at the bottom... Using MVC Attribute Routing in Umbraco Web API Controllers

    Basically add these two classes to your project

     public class AttributeRoutingComponent : IComponent
        {
            public void Initialize()
            {
                GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
    
            }
    
            public void Terminate()
            {
    
            }
        }
    
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class AttributeRoutingComposer : IComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Insert<AttributeRoutingComponent>(); ;
            }
        }
    

    Then your API Method might look something like this..

     [HttpGet]
            [Route("residence/api/getall")]
            public TestDataDto GetAll()
            {
                TestDataDto TestDataDto = new TestDataDto();
                TestDataDto.TestString = "this is a test";
                TestDataDto.AnotherTestString = "this is a another test";
                TestDataDto.TestInt = 99;
                TestDataDto.TestBool = true;
                return TestDataDto;
            }
    

    Regards

    David

  • Nathan Reece 62 posts 376 karma points
    Apr 01, 2021 @ 02:51
    Nathan Reece
    0

    Awesome,

    Thanks a bunch.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies