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 23 posts 63 karma points
    1 day ago
    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 118 posts 564 karma points
    1 day ago
    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 23 posts 63 karma points
    1 day ago
    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!

Please Sign in or register to post replies

Write your reply to:

Draft