Copied to clipboard

Flag this post as spam?

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


  • Sjoerd Stottelaar 31 posts 189 karma points
    Oct 03, 2019 @ 10:10
    Sjoerd Stottelaar
    0

    Get value from document type in master page

    I want to add a canonical URL to my master page which is coming from the document type. So on my document type I have a node picker where I can select a page from the content tree. I want to render this URL in the head section of my master template. Is this possible?

    I've tried many things, the most obvious ones didn't work:

    var canonicalUrl = Model.Value("metaCanonical").Url
    

    Or

    IPublishedContent canonicalUrl = Model.Value("metaCanonical")
    @canonicalUrl.Url
    

    I'm aware that the master page can't inherit any specific model, this is what my master template inherits:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    Any suggestions as to how I can access the page URL from the node picker? Thanks in advance!

  • Mila Pandurska 75 posts 353 karma points
    Oct 04, 2019 @ 08:24
    Mila Pandurska
    100

    Hi Sjoerd, You can achieve this using standard MVC sections. In the master page place this in the head section:

    <head>
     <title>...</title>
    .....
    @RenderSection("Head", false)
    </head>
    

    Then in your tempates which inherits this master page place this:

    @section Head
    {
      Model.Value("metaCanonical").Url
    }
    

    You can name the section as you wish (not necessary to be "Head").

    Mila

  • Sjoerd Stottelaar 31 posts 189 karma points
    Oct 04, 2019 @ 09:46
    Sjoerd Stottelaar
    0

    Thank you for your suggestion, I think we'll go with this solution. We'd need to add the section to every page, but I think it's the only way to get it working. Thanks alot!

Please Sign in or register to post replies

Write your reply to:

Draft