Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Nov 14, 2014 @ 15:00
    Ismail Mayat
    0

    GetByRoute in multilingual site

    Guys,

    Using umbraco 6.1.6.  Its multilingual site, in some of the sites we have multi region eg Belgium so we have structure below please note there are more pages in each region but i am only showing products and this is where i am having an issue with GetByRoute.

    BE

    FR

               - Products

    NL

               - Products

    the top level be is just holding category in this instance it has no domain assigned, the fr and nl are actual root site nodes and have domains set in umbraco.  So for NL we have domain

    somesite/be-nl (nl-BE)

    when you hit that url you get the home page and all good.  You can also hit the products page so

    somesite/be-nl/products/

    again all good. On that page we have some links that point a products search results page, they look like

    somesite/be-nl/products/search/29836-multipurpose/ (search is of type ProductResults the rest of the url does not exist the id part of url is node id to a tag which is a product category)

    for the target page of type ProductResults there is content finder implemented, 

           /// <summary>
            /// Tries to find the content item for product search results        
            /// </summary>
            /// <param name="contentRequest">
            /// The content request.
            /// </param>
            /// <returns>
            /// True if found
            /// </returns>
            public bool TryFindContent(PublishedContentRequest contentRequest)
            {
                if (contentRequest != null)
                {
                    var path = contentRequest.Uri.GetAbsolutePathDecoded();
                    var parts = path.GetSectionsFromUriAbsolutePath().Distinct(StringComparer.InvariantCultureIgnoreCase).ToList(); //just splits on /
                    string finalPath = string.Empty;
                    if (parts.Count > 2)
                    {
                        // Parse the path 2 levels deep
                        var level2Path = string.Concat('/', string.Join("/", parts.Take(2)), '/');
                        finalPath = level2Path;
                        // See if there is a node at this path, and its of type ProductResults
                        var productResultsNode = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute(level2Path);
    
                        if (NoProductResultsNodeFound(productResultsNode))
                        {
                            //try with a level 3 path becuase it may be site like hk-en and hk-zh so its 3 levels
                            var level3Path = string.Concat('/', string.Join("/", parts.Take(3)), '/');
                            productResultsNode = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute(level3Path);
                            finalPath = level3Path;
                            if (NoProductResultsNodeFound(productResultsNode))
                            {
                                return false;
                            }
    
                        }
    
    
                        // OK - we know its a product results page - but we cannot simply return by route as it will return the first match of /products/search found in the tree
                        // Lets current sites, root node and get the Descendants of type ProductResults
                        // And return the first one that contains the original content request's level 2 Path in its Url (e.g. /products/search)
                        var productSearchNode = contentRequest.RoutingContext.UmbracoContext.ContentCache
                                            .GetById(contentRequest.Domain.RootNodeId)
                                            .Descendants(ProductResults.Alias)
                                            .FirstOrDefault(x => x.Url.IndexOf(finalPath) >= 0);
    
                        if (productSearchNode == null)
                            return false;
    
                        contentRequest.PublishedContent = productSearchNode;
    
                    }
    
                    return contentRequest.PublishedContent != null;
                }
    
                return false;
            }

    the route to get item is /be-nl/products/ when you hit this page directly you get a page, in the properties section under alternative links we have /be-nl/products/ however when you do

    contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute(level2Path); // "/be-nl/products"

    you do not get a page and therefore a 404.  Any ideas what is going on here?

  • Stephen 767 posts 2273 karma points c-trib
    Nov 14, 2014 @ 18:36
    Stephen
    2

    Quick (so might not be correct by anyway): a route is "/path/to/page" when there is no domain, and "123/path/to/page" when there is a domain, and then 123 is the ID of the node which is the root of the domain. In your case, you probably want a route such as "123/be-nl/products" where 123 is the ID of the "NL" node. Making sense?

  • Thomas Morris 35 posts 133 karma points MVP c-trib
    Dec 15, 2014 @ 16:48
    Thomas Morris
    1

    Just in case anyone else is having the same issue, Stephen's response is correct. It appears that you need to pass in the root node id of the domain to find the node you want.

    So, from the example above, this becomes...

    contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute(contentRequest.Domain.RootNodeId + level2Path);
    
  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Jan 24, 2017 @ 15:08
    Ali Sheikh Taheri
    0

    This is what I used and it seems to be easier to retrieve the current website root node:

     var currentSiteRootNode = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(contentRequest.Domain.RootNodeId);
    
Please Sign in or register to post replies

Write your reply to:

Draft