Copied to clipboard

Flag this post as spam?

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


  • Peter Laurie 42 posts 116 karma points
    Mar 13, 2019 @ 09:58
    Peter Laurie
    0

    Umbraco 8 - UrlProviderResolver does not exist in the current context

    Hi, I am looking to add extra functionality to Umbraco 8 using Compose, and specifically Umbraco.Web.Routing.UrlProviderResolver Has UrlProviderResolver been removed from Umbraco 8, and if so what replaces this functionality? I am looking to override the Routing with a view to creating our own headless CMS The code snippet is below, but wherever I have tried to implement UrlProviderResolver I have the same error "The name UrlProviderResolver does not exist in the current context"

    Here is my code snippet:

        using AutoMapper;
    using System.Web.Routing;
    using CRest.CMTRest.MappingProfile;
    using CRest.CMTRest.Routing;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Web;
    using Umbraco.Web.Routing;
    
    namespace UMB8_CRest
    {
      public class CRestStartUp : IUserComposer
      {
    
        public void Compose(Composition composition)
        {
          // Append our component to the collection of Components
          // It will be the last one to be run
          composition.Components().Append<MyComponent>();
        }
    
        public class MyComponent : IComponent
        {
          // initialize: runs once when Umbraco starts
          public void Initialize()
          {
            InitAutoMapper();
            InitRouting(); 
          }
    
          public void InitAutoMapper()
          {
            Mapper.Initialize(cfg => {
              cfg.AddProfile<OurRestMappingProfile>();
            });
          }
    
          public void InitRouting()
          {
            RouteTable.Routes.IgnoreStandardExclusions();
            Umbraco.Web.Routing.UrlProvider
            UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, OurRestUrlProvider>();
          }
    
          // terminate: runs once when Umbraco stops
          public void Terminate()
          {
          }
    
        }
    }
    

    The line which is at the core of this issue is: UrlProviderResolver.Current.InsertTypeBefore

    I appreciate that Umbraco 8 is only just out of the blocks, and the documentation is in it early days. Can someone please shed some light on how to handle this in the new Umbraco 8 build. What has replaced UrlProviderResolver, and do you have any examples on how to implement it?

    Thank you and kind regards,

    Peter

  • Simon Andrews 17 posts 131 karma points
    Mar 13, 2019 @ 12:40
    Simon Andrews
    0

    Hi,

    I've been looking into a similar implementation for Umbraco 8 and this is what I've come up with:

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class ApplicationStartUp : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.UrlProviders().InsertBefore<DefaultUrlProvider, MyUrlProvider>();
            composition.ContentFinders().InsertBefore<ContentFinderByUrl, MyContentFinder>();
        }
    }
    
    public class MyUrlProvider : IUrlProvider
    {
        public UrlInfo GetUrl(UmbracoContext umbracoContext, IPublishedContent content, UrlProviderMode mode, string culture, Uri current)
        {
            throw new NotImplementedException();
        }
    
        public IEnumerable<UrlInfo> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
        {
            throw new NotImplementedException();
        }
    }
    
    public class MyContentFinder : IContentFinder
    {
        public bool TryFindContent(PublishedRequest frequest)
        {
            throw new NotImplementedException();
        }
    }
    

    There are 2 issues I'm having though:

    1. For some reason you have to disable the Url tracking, this isn't a big deal for what I'm working on but might be for others
    2. The use of the Url Provider itself can be intermittent on the site restart, for some reason the Umbraco codebase doesn't always use the Provider and the site needs to be restarted to pick it up.

    Hope this helps point you in the right direction.

  • Peter Laurie 42 posts 116 karma points
    Mar 13, 2019 @ 13:16
    Peter Laurie
    0

    Hi Simon, Thank you for this reply, I will look at this later and get back to you with some feedback and my experience of using/developing this code. Kind regards, Pete

  • Simon Andrews 17 posts 131 karma points
    Mar 13, 2019 @ 14:16
    Simon Andrews
    1

    Hi Peter,

    I seem to have resolved the issues I was having so now the code is:

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class UrlProviderStartUp : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.UrlProviders().InsertBefore<DefaultUrlProvider, MyUrlProvider>();
        }
    }
    
    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class ContentFinderStartUp : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.ContentFinders().InsertBefore<ContentFinderByUrl, MyContentFinder>();
        }
    }
    
    
    public class MyUrlProvider : IUrlProvider
    {
        public UrlInfo GetUrl(UmbracoContext umbracoContext, IPublishedContent content, UrlProviderMode mode, string culture, Uri current)
        {
            throw new NotImplementedException();
        }
    
        public IEnumerable<UrlInfo> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
        {
            throw new NotImplementedException();
        }
    }
    
    public class MyContentFinder : IContentFinder
    {
        public bool TryFindContent(PublishedRequest frequest)
        {
            throw new NotImplementedException();
        }
    }
    

    This appears to work everytime now.

Please Sign in or register to post replies

Write your reply to:

Draft