Copied to clipboard

Flag this post as spam?

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


  • Ayo Adesina 430 posts 1023 karma points
    Dec 29, 2022 @ 18:47
    Ayo Adesina
    0

    How to access Current.UmbracoContext from inside UmbracoApiController V10

    I'm rebuidling a Umbraco project to work with V10 from an older V8 build.

    in V8, from inside my API controller (a controller that inherist from UmbracoApiController)

    I could do this:

    Current.UmbracoContext.Content.GetByRoute(pageUrl);
    

    I want to do the same thing in this V10 version, I want to find content by its page URL

    Whats the best way to do this when using UmbracoApiController in V10?

  • Johannes Lantz 156 posts 838 karma points c-trib
    Dec 29, 2022 @ 21:01
    Johannes Lantz
    2

    Hi Ayo!

    I belive you have to do a DI of IUmbracoContextAccessor . Something like this:

    public class FooApiController: UmbracoApiController
    {
        private IUmbracoContextAccessor _umbracoContextAccessor;
        public FooApiController: (IUmbracoContextAccessor umbracoContextAccessor)
        {
            _umbracoContextAccessor = umbracoContextAccessor;
        }
    
        public string Bar()
        {
            var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
            return "";
        }
    }
    

    Hope this gets you on the right track!

    //Johannes

  • Ayo Adesina 430 posts 1023 karma points
    Dec 30, 2022 @ 00:06
    Ayo Adesina
    0

    Thanks Johannes Lantz - Perfect.

Please Sign in or register to post replies

Write your reply to:

Draft