Copied to clipboard

Flag this post as spam?

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


  • SandyK-W 24 posts 64 karma points
    Nov 01, 2024 @ 14:53
    SandyK-W
    0

    Umbraco 13 ServerVariablesParsing

    Hi,

    In version 8 our third-party web folks implemented this code:

    namespace Galbraith.Composers
    {
        public class ReapitServiceComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Register<IReapitService, ReapitService>(Lifetime.Singleton);
    
                ServerVariablesParser.Parsing += ServerVariablesParserOnParsing;
            }
    
            private void ServerVariablesParserOnParsing(object sender, Dictionary<string, object> e)
            {
                if (HttpContext.Current == null) throw new InvalidOperationException("HttpContext is null.");
                var urlHelper =
                    new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData()));
    
                var umbracoUrls = (Dictionary<string, object>)e["umbracoUrls"];
                umbracoUrls.Add("reapitImportUrl", urlHelper.GetUmbracoApiServiceBaseUrl<ReapitImportController>(controller => controller.Import(null, false, null, false)));
                e["umbracoUrls"] = umbracoUrls;
    
            }
        }
    }
    

    I've managed to convert bits of it, but I do not understand the logic behind the server variables in Umbraco 13.

    I've read into the severvariablesparsingnotification but have no idea if it is what I need and I can't seem to work out how to implement it either.

    Any pointers are much appreciated!

  • Garðar Þorsteinsson 119 posts 566 karma points
    Nov 01, 2024 @ 16:27
    Garðar Þorsteinsson
    0

    Hi Sandy,

    In version 13 you could do it like this:

    Register in for example startup

        builder
            .AddNotificationHandler<ServerVariablesParsingNotification, UmbracoEventListeners>();  
    

    Event Listener class

    class UmbracoEventListeners :
        INotificationHandler<ServerVariablesParsingNotification>
    {
        public UmbracoEventListeners()
        {
    
        }
    
        public void Handle(ServerVariablesParsingNotification notification)
        {
            notification.ServerVariables.Add("umbracoUrls", new
            { 
                reapitImportUrl= "baseUrlOfTheReapitImportController",
            });
        }
    

    Hope this gets you on the right track

  • SandyK-W 24 posts 64 karma points
    Nov 01, 2024 @ 16:32
    SandyK-W
    0

    Hi Garðar,

    I'll give it a shot and see if I can get it to do something :)

    Thanks, Sandy

    p.s. will report back and mark it as correct if it moves things forward!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies