Copied to clipboard

Flag this post as spam?

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


  • Damian Green 452 posts 1433 karma points
    Oct 03, 2014 @ 13:36
    Damian Green
    0

    How to get UmbrcoContext in application start handlers and wired up events

    In a class wired up to handle the IApplicationStart handler how do you get an UmbracoContext from either the applicationcontext or the umbracoapplicationbase?

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication,
            ApplicationContext applicationContext)
    

    You cannot call the UmbracoContext.Current from here as it is null and the constructors for the UmbracoContext are internal.

    I need to get a PublishedContent node at this point to get its Url for a redirect.

    I want to get an Umbracohelper but need the UmbracoContext to do so.

    I am currently using Node as a workaround but this is the old way of doing things.... Is there a better way?

    Any ideas how i can get this umbraco context or a published page from the cache at this point in the lifecycle?

    I am actually using this in the PublishedContentRequest.Prepared event wired up in the application started event to be precise.

    Thanks Damian

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Oct 03, 2014 @ 14:52
    Andy Butland
    0

    You should be OK with that Damian I think.  You may not be able to access it directly in the ApplicationStarted event, but you can in the PublishedContentRequest.Prepared event when that is fired from the arguments, e.g. 

        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            PublishedContentRequest.Prepared += PublishedContentRequest_Prepared;
            base.ApplicationStarted(umbracoApplication, applicationContext);
        }
    
        private void PublishedContentRequest_Prepared(object sender, System.EventArgs e)
        {
            var request = sender as Umbraco.Web.Routing.PublishedContentRequest;
            if (request != null && request.HasPublishedContent)
            {
                var content = request.PublishedContent;
                ...
            }
        }
    

    Andy

  • Damian Green 452 posts 1433 karma points
    Oct 03, 2014 @ 16:10
    Damian Green
    0

    Thats not quite what i was after...

    Here is my code as it stands - it just feels a bit dirty using the old Node instead of Umbraco.GetTypedContent(id) for example. What i needed is a way to create an umbrco helper in the event - but UmbracoContext.Current is unavailable.

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication,
            ApplicationContext applicationContext)
        {
            PublishedContentRequest.Prepared += PublishedContentRequest_Prepared;
        }
    
        private void PublishedContentRequest_Prepared(object sender, EventArgs e)
        {
            // Check to make sure the request is valid
            var request = sender as PublishedContentRequest;
            if (request == null || !request.HasPublishedContent)
                return;
    
            //See if member is in lapsed role
            //
            var lapsed = Roles.IsUserInRole("Lapsed");
    
            if (!lapsed) return;
    
            var pageIsProtected = Access.IsProtected(request.PublishedContent.Id, request.PublishedContent.Path);
    
    
            // Bounce to banned page
            //
            if (pageIsProtected)
            {
                const int lapsedId = 4497;
    
                Node lapsedNode = null;
    
                lapsedNode = new Node(lapsedId);
                var lapsedUrl = "/account/lapsed";
    
                try
                {
                    lapsedUrl = lapsedNode.Url;
                }
                catch (Exception)
                {
                }
    
                request.SetRedirect(lapsedUrl, 302);
            }
        }
    
  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Oct 03, 2014 @ 17:08
    Andy Butland
    0

    I see.  Well another option might be to query from the published content - something like:

    var lapsedPageUrl = request.PublishedContent
    .AnscestorsOrSelf(1)
    .Descendents("LapsedPageDocTypeAlias")
    .First().Url;  

     (that query is off the top of my head so almost certainly wrong... but illustrates the point).

    Andy

  • Damian Green 452 posts 1433 karma points
    Oct 03, 2014 @ 17:18
    Damian Green
    0

    Thought about that but its a generic type im looking for as a specific page so i guess i could search on name but too flaky.

    Ill leave it using Node for now but don't know why UmbracoContext.Current is null when the PublishedContentRequest.Prepared event fires! ??

    Maybe ill ask Stephan and im sure he can shed some light on it.

    Cheers Andy.

    Damian

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Oct 03, 2014 @ 17:22
    Andy Butland
    0

    Good idea!  Do update back if you get a solution, I'd be interested to hear.

  • Damian Green 452 posts 1433 karma points
    Oct 03, 2014 @ 18:01
    Damian Green
    100

    Ok - im a plonker....

    Had a hunch i was wrong and after trying again i was right...

    You CAN use UmbracoContext.Current within the Prepare event! I must have been trying to use it in the Startup handler previously!

    So ive got rid of the node and moved to the proper way! now! :)

    private void PublishedContentRequest_Prepared(object sender, EventArgs e)
        {
            // Check to make sure the request is valid
            var request = sender as PublishedContentRequest;
            if (request == null || !request.HasPublishedContent)
                return;
    
            //See if member is in lapsed role
            //
            var lapsed = Roles.IsUserInRole("Lapsed");
    
            if (!lapsed) return;
    
            var pageIsProtected = Access.IsProtected(request.PublishedContent.Id, request.PublishedContent.Path);
    
    
            // Bounce to banned page
            //
            if (pageIsProtected)
            {
                const int lapsedId = 4497;
                var lapsedUrl = "/account/lapsed";
    
                try
                {
                    var helper = new UmbracoHelper(UmbracoContext.Current);
                    var lapsedNode = helper.TypedContent(lapsedId);
    
                    if (lapsedNode != null)
                        lapsedUrl = lapsedNode.Url;
                }
                catch (Exception)
                {
                }
    
                request.SetRedirect(lapsedUrl, 302);
            }
        }
    
  • Dennis Milandt 190 posts 517 karma points
    Mar 01, 2015 @ 22:31
    Dennis Milandt
    0

    But PublishedContentRequest_Prepared runs for every request? Was that the intention?

    I have been looking for a way to access the Umbraco context in the startup events, but with no luck so far.

    / Dennis

  • Bruno 23 posts 53 karma points
    Oct 13, 2015 @ 10:51
    Bruno
    0

    I am trying to do someting like Articulate Git at line 49. In my case i wanna construct my url by concatenating to specifc nodes. for instance i wanna check all nodes of type X, then loop and create a route for each of them....

    someone have an idea how to achieve this ?

Please Sign in or register to post replies

Write your reply to:

Draft