Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi. I'm still really new to Umbraco and Razor.
I want to retrieve the URL of a specific page ("Project A"). In my content tree the page is nested as such:
Home > Projects > Project A
I have already used the following code to retrieve the URL of a page ("About"), that's on the same level as "Projects":
var aboutUrl = Model.Root().ChildrenOfType("about").FirstOrDefault().Url;
But I believe I have to go one level deeper now.
Hope you can help.
There's a lot of ways of doing this.
If the page is always going to be the same you could hardcode the Id or Guid.
If you want to stay in the same vein...
var projectAUrl = Model.Root().ChildrenOfType("projects").FirstOrDefault().ChildrenOfType("project").Where(x => x.Name == "Project A").FirstOrDefault().Url;
Assuming the doc type for Projects is "projects" and Project A is "project". Change as appropriate.
I'm getting an error, when using this on a website with multiple languages. I believe it's because of using the name, which changes depending on the language:
.Where(x => x.Name == "Project A").
But I tried removing that part and it seems to work fine. Is there any reason why that part has to be there?
As I said- there are a lot of ways of doing this.
If you're sure that it's always going to be the first project in the sort order then you don't need the name check.
Thanks! This is exactly what I was looking for.
I initially wanted to use the ID, but I believe IDs are different between environments.
They are if you use Courier and once you deploy.
If you're creating a website and then will later clone the DB to create a live instance it's usually OK to use the IDs or Guids of the content skeleton.. if you create the projects folder it will always be id 1234.
There might be a slight performance hit on searching for this node everytime rather than going straight to it.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted