Copied to clipboard

Flag this post as spam?

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


  • Aspargeus 12 posts 102 karma points
    Sep 18, 2020 @ 07:55
    Aspargeus
    0

    Getting a specific page URL

    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.

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Sep 18, 2020 @ 12:42
    Steve Morgan
    100

    Hi,

    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.

  • Aspargeus 12 posts 102 karma points
    Sep 24, 2020 @ 09:51
    Aspargeus
    0

    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?

    Thanks.

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Sep 24, 2020 @ 13:37
    Steve Morgan
    0

    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.

  • Aspargeus 12 posts 102 karma points
    Sep 24, 2020 @ 14:16
    Aspargeus
    0

    Ok. Thanks.

  • Aspargeus 12 posts 102 karma points
    Sep 18, 2020 @ 13:30
    Aspargeus
    0

    Thanks! This is exactly what I was looking for.

    I initially wanted to use the ID, but I believe IDs are different between environments.

    Thanks again.

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Sep 18, 2020 @ 14:22
    Steve Morgan
    0

    Hi,

    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.

    Steve

Please Sign in or register to post replies

Write your reply to:

Draft