Copied to clipboard

Flag this post as spam?

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


  • wmotr 16 posts 146 karma points
    Jul 20, 2021 @ 18:54
    wmotr
    0

    Access Current Page using DI and UmbracoContextFactory

    Hi forum and friends,

    I am creating an umbraco that gives possibility to host multiple websites. I use a pattern where Settings are stored as pages and Blocks, same for Menus (as Pages) that are all descendants of Home.

    I then have multiple homes, within folders to organize them.

    My issue is that code is been reused for each site : I have a MenuController (vanilla Controller) Registered with MapRoute and correctly loaded when I call a @Html.Action

    Now, In this Controller, I need to access the Settings Page within the Home node I am.

    My idea was to do something like : currentNode . AncestorOrSelf("home").ChildrenOfType("settings").

    errr I don't know where to get "currentNode" within the controller.

    And this RenderMenu Html.Action is called from master, right before RenderBody(). When I try to pass the Model to Html.Action, and cast it into IPublishedContent, it's a null value.

    Thanks guys for your help !!

    Definitely looking forward to trying v9

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jul 23, 2021 @ 21:48
    Nik
    0

    Hi WMOTR,

    You could have your controller action accept a guid parameter and pass in the key of the current page, or the key of the current page's root home node. I'm assuming your Master template is inheriting from UmbracoViewPage so it knows the current page it is rendering.

    Thanks

    Nik

  • wmotr 16 posts 146 karma points
    Jul 24, 2021 @ 17:11
    wmotr
    100

    Hi @Nik, Thanks for answering

    I found out by myself this week a solution :

    @Html.Action("RenderHeader", "RenderElements", Model.Page)

    in RenderElementsController :

    public ActionResult RenderHeader(dynamic data){

    and passing in my HeaderViewModel a : _homepage.GetHomePage(data as IPublishedContent).Url(),

    where _homepage is a IHomePageService that I get from DI, and GetHomePage a method Where I reference IUmbracoContextFactory to gather the homepage starting from the data param passed. A bit tricky and complicated I assume. Could be better...

    What do you think of this solution ?

    I just have to make sure that the dynamic data is the right type of Object.

    If expecting a IPublishedContent in : public ActionResult RenderHeader(IPublishedContent data){

    The data object is null every time...

    Thanks for taking the time to answer, again

    Sylvain

Please Sign in or register to post replies

Write your reply to:

Draft