How to get the correct root node for a mutisite Umbraco install?
m
Up until now my instance of Umbraco only housed one website so to get the root node I would simply use the code: var context = _umbracoContextAccessor.GetRequiredUmbracoContext(); var site = context.Content.GetAtRoot().First(); But now I will be adding adtional sites to the instance which will be using the same doc types and templates as the existing site. So how can I get the correct root node for the site the user is visiting?
j
You will need some sort of context for the page the visitor is on, and the way to do it will depend on what type of code you are calling from. A RenderController fx will have the
CurrentPage
property which is an IPublishedContent, so based on that you could navigate to the site node. From fx an ApiController you would have no context, so the spot you call the api controller from would often pass along fx a current node id to the api controller to get the node and then navigate from
m
For right now the code would be excuted as part of the Master Template file. The code is deisgned to go to the root node of the site and find the child named "siteNavigation" so it can render the navigation so not inside controller but more inside the razor page.
j
Alright, so in the master template you should have access to the page model via the
Model
variable. So a common way to do this if you use modelsbuilder models would be to navigate up the tree to the correct model like this:
Copy code
csharp
var site = Model.AncestorOrSelf<ContentModels.Site>();
or without modelsbuilder:
Copy code
csharp
var site = Model.AncestorOrSelf("site");
Assuming your root node is of the type "Site", otherwise it should of course be adapted to your case
m
I think I got it. Thanks
481 Views