Copied to clipboard

Flag this post as spam?

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


  • Nicolas Erlijman 14 posts 103 karma points
    Jan 26, 2021 @ 20:04
    Nicolas Erlijman
    0

    WebApi controllers without umbraco/api

    I have a legacy webapi SomeController that I have to integrate to umbraco front end.

    It is working changing inheritance from ApiController to Umbraco.Web.WebApi.UmbracoApiController

    and calling it with umbraco/api/Some instead of api/Some

    Is there any way to call it without umbraco/ ?

    E.g. api/Some/MyAction ?

    Thanks!

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 26, 2021 @ 22:52
    Nik
    0

    Hi Nicolas,

    You can enable Attribute Routing to achieve something like this. Depending on your requirements you can either set the RoutePrefix at a controller level, or you can set routes on a per action basis.

    You'll need a composer/component (I can't remember if you need one or both off the top of my head), in order to enable attribute routing.

    I'll try and pop back tomorrow and share some sample code but this might point you in the right direction initially.

    Cheers

    Nik

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 27, 2021 @ 07:59
    Nik
    101

    Hi Nicolas,

    As mentioned here is some sample code:

    Composer

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class AttributeRoutingComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Insert<AttributeRoutingComponent>();
        }
    }
    

    Component

    public class AttributeRoutingComponent : IComponent
    {
        public AttributeRoutingComponent()
        {
            GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
        }
        public void Initialize()
        {
    
        }
    
        public void Terminate()
        {
    
        }
    }
    

    Then you can have an API controller that looks like this:

    public class MyApiController : UmbracoApiController
    {
            [Route("/api/v1/feed/basic")]
            public object MyEndPoint()
            {
            }
    }
    

    This would then be accessible on the url /api/v1/feed/basic

    Hope that helps :-)

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft