Copied to clipboard

Flag this post as spam?

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


  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Nov 01, 2016 @ 11:11
    Michaël Vanbrabandt
    0

    NiceUrlWithDomain issue using custom UrlProvider

    Hi all,

    on my master page I need to set a canonical url for some pages because we can have multiple urls pointing to the same content.

    <link href="@Umbraco.NiceUrlWithDomain(CurrentPage.Id)" rel="canonical" />
    

    For most pages it is working fine but for my Categories and Companies, which are using custom urls by defining a custom UrlProvider this doesn't work.

    I only get for instance: /categorie-1/ or /company-1/ so the domain isn't defined.

    Below are my url providers:

    Compagnies

    public class CompaniesUrlProvider : IUrlProvider
    {
        public IEnumerable<string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
        {
            return Enumerable.Empty<string>();
        }
    
        public string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
        {
            var content = umbracoContext.ContentCache.GetById(id);
            if (content != null && (content.DocumentTypeAlias == "companyItem"))
            {
                return "/" + content.UrlName + "/";
            }
    
            return null;
        }
    }
    

    Categories

    public class CategoriesUrlProvider : IUrlProvider
    {
        public IEnumerable<string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
        {
            return Enumerable.Empty<string>();
        }
    
        public string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
        {
            var content = umbracoContext.ContentCache.GetById(id);
            if(content != null && (content.DocumentTypeAlias == "primaryCategory" || content.DocumentTypeAlias == "subCategory"))
            {
                return "/" + content.UrlName + "/";
            }
    
            return null;
        }
    }
    

    Registrating url providers and contentfinders

        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, CategoriesUrlProvider>();
            UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, CompaniesUrlProvider>();
    
            ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNiceUrl, CategoriesContentFinder>();
            ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNiceUrl, CompaniesContentFinder>();
        }
    

    Anyone that can help me out here?

    Is there a way to know in the url provider when the url needs to be prefixed by the domain of the website and how can I get this domain?

    /Michaël

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Nov 02, 2016 @ 09:12
    Dave Woestenborghs
    0

    Hi Michaël,

    Maybe you can change can do it through umbracoSettings.config

     <requestHandler>
        <!-- this will ensure that urls are unique when running with multiple root nodes -->
        <useDomainPrefixes>true</useDomainPrefixes>
        <!-- this will add a trailing slash (/) to urls when in directory url mode -->
        <addTrailingSlash>true</addTrailingSlash>   
      </requestHandler>
    

    This will make sure that a domain is included in the url and that the url has a trailing slash. So you don't need to do that in your urlprovider.

    Dave

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Nov 11, 2016 @ 19:01
    Michaël Vanbrabandt
    0

    Hi Dave,

    sorry for the late response.

    I have tried this but this doesn't work. It still keeps showing the url without the domain.

    Any ideas?

    /Michaël

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Nov 16, 2016 @ 06:52
    Dave Woestenborghs
    0

    I think for getting the full url you need to set a domain on your root node

    Dave

  • Marcio Goularte 388 posts 1360 karma points
    Nov 14, 2016 @ 12:51
    Marcio Goularte
    0

    Hi,

    I can not tell you why NiceUrlWithDomain is not working. Does this happen in the development environment? localhost?

    For canonical, in razor do this:

    var canonicalUrl = String.Empty;
    if (Request.ServerVariables["HTTP_HOST"].ToLower().StartsWith("www"))
    {
        canonicalUrl = "http://" + Request.ServerVariables["HTTP_HOST"].ToString() + Model.Content.Url;
    }
    else
    {
        canonicalUrl = "http://www." + Request.ServerVariables["HTTP_HOST"].ToString() + Model.Content.Url;
    }
    
    <link rel="canonical" href="@canonicalUrl" />
    
  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Nov 14, 2016 @ 12:55
    Michaël Vanbrabandt
    0

    Hi Marcio,

    I am implementing custom UrlProviders which are included before the nice url provider.

    So that why it only shows the /categorie-1/ or /company-1/ url.

    I was also thinking of using this but I would like to know how this can be done in the url provider instead of doing it in the view.

    /Michaël

  • Marcio Goularte 388 posts 1360 karma points
    Nov 14, 2016 @ 13:43
    Marcio Goularte
    0

    I believe that including the domain, for canonical, in the url is only in view, template, partial view, etc. UrlProvider is for the URL after the domain. Also because there could be multiple domains and doing so in UrlProvider would bring problems. I may be wrong. I am used to doing canonical in view with razor.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Nov 16, 2016 @ 10:29
    Michaël Vanbrabandt
    0

    Hi Marcio,

    the complete url is rendered by the UrlProvider.

    I have found that it passes a param called UrlProviderMode which indicated weither the url needs the domain prefix or not.

    The only issue is catching the umbraco settings config file to get the value of the UseDomainPrefixes.

    /Michaël

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Nov 15, 2016 @ 21:06
    Michaël Vanbrabandt
    1

    Ok after some scrolling into the Umbraco code I have now implemented a complete custom IUrlProvider where I check the UrlProviderMode to know if the request needs the domain prefix or not.

    This is working fine, but I now need to check the UseDomainPrefixes property which can be accessed by the IRequestHandlerSection.

    But if I implement this using the constructor like the DefaultUrlProvider then the .GetUrl is never called.

        private readonly IRequestHandlerSection _requestSettings;
    
        public CompaniesUrlProvider(IRequestHandlerSection requestSettings)
        {
            _requestSettings = requestSettings;
        }
    

    I also see that IRequestHandlerSection is implemented by the RequestHandlerElement, which is internal.

    So we can't access this in our project.

    Seems like we get stuck here to get a full working custom url provider.

    /Michaël

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Mar 16, 2017 @ 09:19
    Chriztian Steinmeier
    0

    Hi Michaël,

    Were you ever able to "fix" this and have your custom UrlProvider work for NiceUrlWithDomain()?

    /Chriztian

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 16, 2017 @ 09:56
    Michaël Vanbrabandt
    1

    Hi Chriztian,

    yes I got it working but it has been a pain in the ass!

    Also its not a perfect solution, because I had to copy code from the core because it was all internal ( so not possible to use it for custom providers ).

    Was already looking to make a PR for this to make it more useable but haven't got the time yet to make it work.

    /Michaël

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Mar 16, 2017 @ 10:02
    Chriztian Steinmeier
    1

    Okay - I was fortunate that my custom UrlProvider used the parent node's URL as its base, so I was able to pass the provided UrlProviderMode argument along when getting that, so it just magically worked.

    But other UrlProviders may not always be able to do that...

    /Chriztian

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 16, 2017 @ 10:10
    Michaël Vanbrabandt
    0

    I will try to make a blog post about the problem and solution and add it here so others can use it.

    Maybe then you can add your solution also about using the parent node url.

    I will keep you posted!

    /Michaël

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Mar 16, 2017 @ 14:08
Please Sign in or register to post replies

Write your reply to:

Draft