Copied to clipboard

Flag this post as spam?

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


  • Keith 12 posts 91 karma points
    Apr 16, 2024 @ 16:55
    Keith
    0

    How to reuse Current Page in a class?

    Hi

    Umbraco 13

    I would like to create a class which carries out a few tasks.

    At the moment, for all the tasks in this class, I need to pass in CurrentPage after the controller that uses this inherits from SurfaceController.

    Example

    Controller

    Public myClassController : SurfaceController

    My class

    Public class TestClass

    Public void MethodOne(IPublishedContent currentpage)

    Is there anyway my test class could have IPublishedContent passed in (as in the current page) to use across all the methods, rather than having each method declared with a parameter to be passed in?

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Apr 17, 2024 @ 22:15
    Marc Goodson
    0

    Hi Keith

    It sounds like you want your custom TestClass service to be dependent on the current UmbracoContext?

    There is a good article here about services and helpers and how you can access the UmbracoContext in different circumstances...

    https://docs.umbraco.com/umbraco-cms/implementation/services

    Essentially if your custom Test Class is registered with DI, it can have UmbracoContextFactory injected into its constructor..

    ... And you can use this to get the Current UmbracoContext from which you can access the Current Page from

    UmbracoContext?.PublishedRequest?.PublishedContent

    So if your Test Class gets the Umbraco Context in its constructor you can read the current page and assign it to a private property of the class that the individual methods can then access...

    If you are only calling it from a SurfaceController, then you have access to the current page via

    Umbraco. AssignedContentItem

    It all depends on how you are using it

    Hope this gives you some ideas!

    Regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft