Copied to clipboard

Flag this post as spam?

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


  • Rasmus Eeg 91 posts 457 karma points c-trib
    Feb 26, 2019 @ 20:50
    Rasmus Eeg
    1

    Getting the current UmbracoContext in v8?

    So i have the following code which does not work any more:

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    

    How do i create a new instance of the UmbracoHelper in v8?

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Feb 26, 2019 @ 23:00
    Søren Gregersen
    0

    Short answer: you don’t

    Long answer: the services are accessed via dependency injection. Either you just have it as a parameter on your class’ constructor, or you use Current.Factory.GetInstance

  • Rasmus Eeg 91 posts 457 karma points c-trib
    Feb 27, 2019 @ 04:39
    Rasmus Eeg
    0

    Is the current a static class somewhere? I'm trying to access it from outside any umbraco view or controller.

    Do you know if there is any documentation or topics on this?

  • Nathan Woulfe 447 posts 1664 karma points MVP 5x hq c-trib
    Feb 27, 2019 @ 04:54
    Nathan Woulfe
    1

    Yup, static class. It's in Umbraco.Web.Composing. A version also exists in Umbraco.Core.Compsing, but exposes different stuff.

    More than likely you'll be chasing the Umbraco.Web.Composing one.

  • Nathan Woulfe 447 posts 1664 karma points MVP 5x hq c-trib
    Feb 27, 2019 @ 01:58
    Nathan Woulfe
    102

    UmbracoHelper is still available though - it's a property on Current. From a quick look though, I don't think it has all the same methods as in v7.

    Depending on what you're trying to do, Current.UmbracoContext.ContentCache has some useful bits too.

    Definitely have a poke around in Current.

  • Rasmus Eeg 91 posts 457 karma points c-trib
    Feb 27, 2019 @ 07:25
    Rasmus Eeg
    2

    Thank you! Umbraco.Web.Composing.Current.UmbracoHelper is the new way.

  • Daniel Bardi 927 posts 2562 karma points
    Dec 03, 2019 @ 21:07
    Daniel Bardi
    2

    Umbraco.Web.Composing.Current.UmbracoContext

  • Peter Sinke 11 posts 82 karma points
    Mar 04, 2019 @ 20:30
    Peter Sinke
    1

    When I try to do this (Umbraco.Web.Composing.Current.UmbracoHelper) from a class library 'Current' is null. How do you get the umbracoHelper in a class library in v8?

  • Markus Johansson 1911 posts 5757 karma points MVP c-trib
    Mar 19, 2019 @ 16:00
    Markus Johansson
    5

    I ended up here after looking for a way to get the UmbracoContext,

    The old way would be UmbracoContext.Current,

    After some looking around I found this that seems to work for me

    DependencyResolver.Current.GetService<IUmbracoContextFactory>().EnsureUmbracoContext().UmbracoContext
    

    Just wanted to share this is anyone else have the same needs.

    // m

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Mar 19, 2019 @ 16:50
    Søren Gregersen
    0

    I would not recommend this. You should read up on dependency injection.

    When you want to use UmbracoContext, you Almost always are in a context where you already have it. If you are building some static method to do something reusable, just give the context as an argument.

  • Markus Johansson 1911 posts 5757 karma points MVP c-trib
    Mar 19, 2019 @ 18:04
    Markus Johansson
    3

    Hi Søren!

    I’m very aware of DI and IOC so I don’t think I need to “read up” on that. Please don’t assume that someone that posts something that you (without context) don’t agree with don’t know what they are doing.

    Service Locator is an anti pattern but it’s not illegal to use when it makes more sense.

    I agree with you on a general level, injecting dependencies is always best and should always be preferred.

    In my case the creation of the instance is out of my (and the IOC Contains) control and it only accepts a parameterless constructor, the method calls is also specified in external code so Service Locator is the least bad solution.

    All the best.

  • Kimbrough 11 posts 141 karma points
    Jun 25, 2019 @ 18:27
    Kimbrough
    0

    For anyone with this problem in the future and using a view with UmbracoContext.Current from v7.

    I went from this v7 code:

    @model Umbraco.Forms.Mvc.Models.FieldViewModel
    @{
        var memberShipHelper = new MembershipHelper(UmbracoContext.Current);
        var member = memberShipHelper.GetCurrentMember();
    
        // Code using member
    }
    

    To this for v8:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco.Forms.Mvc.Models.FieldViewModel>
    @{
        var member = (myMemberModel)Members.GetCurrentMember();
    
        // Code using member
    }
    

    Services are also available in this partial where I am unable to use DI.

  • Daniel Bardi 927 posts 2562 karma points
    Dec 03, 2019 @ 21:08
    Daniel Bardi
    1

    Umbraco.Web.Composing.UmbracoContext

Please Sign in or register to post replies

Write your reply to:

Draft