Copied to clipboard

Flag this post as spam?

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


  • Jakob Bagterp 13 posts 115 karma points c-trib
    May 18, 2023 @ 17:34
    Jakob Bagterp
    0

    How to get hostname part of a page URL?

    Let's imagine a page with /some/page as path and an absolute URL of https://www.example.com/some/page.

    How do I isolate and get the hostname part including scheme https://www.example.com dynamically?

  • Jakob Bagterp 13 posts 115 karma points c-trib
    May 18, 2023 @ 18:01
    Jakob Bagterp
    101

    I figured out a simple solution based on the standard Umbraco library. Take the absolute URL of a page https://www.example.com/some/page and trim out the relative part /some/page so only the hostname https://www.example.com is left. It should work no matter which page node you feed into it:

    using Umbraco.Cms.Core.Models.PublishedContent;
    
    string absoluteUrl = Model.Url(mode: UrlMode.Absolute);
    string relativePath = Model.Url(mode: UrlMode.Relative);
    string hostname = absoluteUrl.TrimEnd(relativePath);
    

    Also, I found a few other solutions courtesy of Paul Seal that might be relevant to others.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies