Copied to clipboard

Flag this post as spam?

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


  • John Bergman 483 posts 1132 karma points
    Oct 19, 2017 @ 03:15
    John Bergman
    0

    Virtual Nodes, how to determine the correct root site node from RequestContext

    I am working in a multi-site umbraco, so I have multiple root nodes for multiple domains. I have a need to create virtual rows (deriving from UmbracoVirtualNodeRouteHandler).

    In the FindContent() method, I have access to the RequestContext and the umbracoContext. (see below).

    My question is, given this information, how do I determine which root node corresponds to the request?

    protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
    
  • John Bergman 483 posts 1132 karma points
    Oct 27, 2017 @ 17:27
    John Bergman
    0

    bump. Anyone have any ideas about this?

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Oct 28, 2017 @ 11:33
    Marc Goodson
    101

    Hi John

    I'm not sure I quite understand what you are trying to do, but I think what you are saying is you MapUmbracoRoute rule is 'relative' and therefore is matched on all of the sites, which you might want to be the case, but you need to know when you associate the relevant Umbraco Node with the route - which site the incoming request is intended for?

    In pure MVC you can create a DomainRoute for this kind of scenario: https://blog.maartenballiauw.be/post/2015/02/17/domain-routing-and-resolving-current-tenant-with-aspnet-mvc-6-aspnet-5.html but this won't work with MapUmbracoRoute as much as far as I can tell...

    ... Anyway one thing to be aware of is the UmbracoContext object that gets passed to your UmbracoVirtualNodeRouteHandler, and this contains a property containing the original request Url.

    enter image description here

    This will give you the domain of the request, and 'you could' use the Umbraco DomainService to find the root node id of the site corresponding to the requested domain, to give you your site context:

    eg

    int? rootContentId;
    var ds = ApplicationContext.Current.Services.DomainService;
    IDomain matchingDomain = ds.GetByName(domainName);
    if (matchingDomain!=null){
     rootContentId = matchingDomain.RootContentId;
    }
    

    I say 'could' because the DomainService will make a SQL request, and that's likely to slow down your site if it occurs on every request, depending on the makeup of your multi-tenancy site and how often urls change, it may be more pragmatic, to just have a simple lookup dictionary here to map domain to umbraco id, eg if domain == "fish.com" then rootId is 22....

    The other approach would be to map a specific relative route for each site eg:

          RouteTable.Routes.MapUmbracoRoute(
                "Product list by category for uk",
                "uk/category/{id}",
                new
                {
                    controller = "Category",
                    action = "ProductsByCategory",
                    id = UrlParameter.Optional,
                    page = 1,
                    site = "UK",
                },
                new UmbracoYourVirtualNodeRouteHandler("UK"));
        }
          RouteTable.Routes.MapUmbracoRoute(
                "Product list by category for usa",
                "us/category/{id}",
                new
                {
                    controller = "Category",
                    action = "ProductsByCategory",
                    id = UrlParameter.Optional,
                    page = 1,
                    site = "USA",
                },
                new UmbracoYourVirtualNodeRouteHandler("USA"));
        }
    

    etc

    anyway is that close to what you were asking, apologies if I've missed the gist!

    regards

    Marc

  • John Bergman 483 posts 1132 karma points
    Nov 10, 2017 @ 15:24
    John Bergman
    0

    Thanks Marc, that lead me to where I needed to go. :-)

Please Sign in or register to post replies

Write your reply to:

Draft