Copied to clipboard

Flag this post as spam?

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


  • Mus'ab 157 posts 385 karma points notactivated
    Jun 15, 2020 @ 10:14
    Mus'ab
    0

    Hi

    I want to control the nodes URL and I tried UmbracoUrlName property and tried IUrlSegmentProvider like bellow

    namespace AlraiProject.Routing
    {
        public class ProductPageUrlSegmentProvider : IUrlSegmentProvider
        {
    
            readonly IUrlSegmentProvider _provider = new DefaultUrlSegmentProvider();
    
            public string GetUrlSegment(IContentBase content, string culture = null)
            {
                //only apply this rule for product pages
                if (content.ContentType.Alias != "newsItem") return null;
                var segment = _provider.GetUrlSegment(content);
                // get unique product sku/id  to add to url segment
    
                return "musab";
            }
        }
    }
    namespace AlraiProject.Composers
    {
        public class RegisterCustomSegmentProviderComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.UrlSegmentProviders().Insert<ProductPageUrlSegmentProvider>();
            }
        }
    }
    

    but the problem that is I can only control the latest level of URL only for example, if my node-link is domain/news/news-item I can control the news-item and rename it but can't edit news rename or remove is there any solution that ic can control the entire link?

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Jun 15, 2020 @ 17:48
    Marc Goodson
    0

    Hi Mus'ab

    Adding a property called umbracoUrlAlias, would allow you to specify an alternative complete Url for the content item, (the normal Url would still exist - so think about having a canonical tag in the html if you do this). This makes it possible to create Urls that clash with each other, but if the editors are aware of this then this 'just works'...

    https://our.umbraco.com/Documentation/Reference/Routing/routing-properties#umbracourlalias

    Alternatively you could look to implement a Custom Url Provider + ContentFinder combination... there is an example here of creating a Custom Url Provider that inherits from the DefaultUrlProvider, and shows manipulating the Url, with the minimum effort:

    https://our.umbraco.com/Documentation/Reference/Routing/Request-Pipeline/outbound-pipeline#example-1

    If that's what you are after?

    regards

    Marc

  • Mus'ab 157 posts 385 karma points notactivated
    Jun 16, 2020 @ 07:00
    Mus'ab
    0

    Hi Marck

    I tried to use umbracoUrlAlias and the link created successfully but the is a problem when I using it

    the main URL returned instead of custom one when I get it through publish content but I do like the example

    using Umbraco.Core.Logging; using System; using System.Collections.Generic; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Models.PublishedContent; using Umbraco.Web; using Umbraco.Web.Routing; using Umbraco.Core.Composing; using AlraiProject.Routing.UrlProviders;
    
    namespace AlraiProject.Routing.UrlProviders {
    
        public class ProductPageUrlProvider : DefaultUrlProvider
        {
            public ProductPageUrlProvider(IRequestHandlerSection requestSettings, ILogger logger, IGlobalSettings globalSettings, ISiteDomainHelper siteDomainHelper) : base(requestSettings, logger, globalSettings, siteDomainHelper) { }
            public override IEnumerable<UrlInfo> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
            {
                //add custom logic to return 'additional urls' - this method populates a list of additional urls for the node to display in the Umbraco backoffice
                return base.GetOtherUrls(umbracoContext, id, current);
            }
    
            public override UrlInfo GetUrl(UmbracoContext umbracoContext, IPublishedContent content, UrlMode mode, string culture, Uri current)
            {
                //only apply this to product pages
                if (content != null && content.ContentType.Alias == "newsItem")
                {
                    // get the original base url that the DefaultUrlProvider would have returned, it's important to call this via the base, rather than .Url, or UrlProvider.GetUrl to avoid cyclically calling this same provider in an infinite loop!!)
                    UrlInfo defaultUrlInfo = base.GetUrl(umbracoContext, content, mode, culture, current);
                    if (!defaultUrlInfo.IsUrl)
                    {
                        //this is a message (eg published but not visible because the parent is unpublished or similar)
                        return defaultUrlInfo;
                    }
                    else
                    {
                        //manipulate the url somehow in a custom fashion:
                        var originalUrl = defaultUrlInfo.Text;
                        var customUrl = originalUrl + "fish/";
                        return new UrlInfo(customUrl, true, defaultUrlInfo.Culture);
    
                    }
                }
                //otherwise return the base GetUrl result:
                return base.GetUrl(umbracoContext, content, mode, culture, current);
            }
        } }
    
    
    namespace AlraiProject.Composers {
        public class RegisterCustomUrlProviderComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.UrlProviders().Insert<ProductPageUrlProvider>();
            }
        } }
    

    and solve it but the new link get a page not found

    and if I remove the property value all links disappeared and the node looks like this ( This document is published but its URL cannot be routed )

    what do you think should do?

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Jun 16, 2020 @ 07:59
    Marc Goodson
    100

    Hi Mus'ab

    They are two different options, umbracoUrlAlias is based in to Umbraco and just works, if this suits your requirements no need to create a custom UrlProvider...

    The custom UrlProvider approach is an alternative, and gives you more control over how the Url could be defined - but it is only one half of the solution - the UrlProvider will provide the rule for generating the Url based on the content item - however you need to implement an IContentFinder that will take that Url and map it to the ContentItem, otherwise you'll get the message that your shiny new URL cannot be routed!

    https://our.umbraco.com/Documentation/Reference/Routing/Request-Pipeline/IContentFinder

    if that makes sense, but if the umbracoUrlAlias 'does the job' no need to extend the Url Provider + ContentFinder...

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft