Copied to clipboard

Flag this post as spam?

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


  • David Armitage 485 posts 2042 karma points
    Jun 14, 2021 @ 12:33
    David Armitage
    0

    Umbraco 9 Composing Inside Custom Classes and Razor without Dependency Injection

    Hey All,

    Does anyone know how to compose services and helpers inside custom classes or razor views without using dependency injection.

    As a reminder the old way we used to do that i Umbraco 8 was like the following..

    @using Current = Umbraco.Web.Composing.Current IBlogHelper _blogHelper = Current.Factory.GetInstance

    Regards

    David

  • Bjarke Berg 29 posts 265 karma points hq
    Jun 14, 2021 @ 12:51
    Bjarke Berg
    100

    You cannot.. (and should not :P)

    Dependency injection (DI) is supported in the web framework, so in views you can just do something like

    @inject IBlogHelper _blogHelper 
    

    DI is also supported for filters etc.

  • David Armitage 485 posts 2042 karma points
    Jun 17, 2021 @ 07:09
    David Armitage
    0

    Thanks Bjarke.

    I will give this a go. Its great that these now get injected into razor views like this now.

Please Sign in or register to post replies

Write your reply to:

Draft