How do I get the "name" in name.domain.com when rendering a page?

David Thielen 3,121 Reputation points
2024-07-08T23:08:43.7366667+00:00

Hi all;

I am trying to set my app up so that an account, which has a uniqueId can access the webpage as uniqueId.domain.com and I then display just their content.

How do I get the uniqueId from uniqueId.domain.com/folder/page? Do I use NavigationManager? And if so, exactly how? (I worry I parse the url in a way that works - 95% of the time.)

thanks - dave

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,595 questions
0 comments No comments
{count} votes

Accepted answer
  1. Brando Zhang-MSFT 3,706 Reputation points Microsoft Vendor
    2024-07-09T01:55:43.25+00:00

    Hi @David Thielen,

    Yes, you could use the NavigationManager to get the domain name, and then use the host to split the domain and get the name.

    More details, you could refer to below codes:

    @inject NavigationManager Navigation
    
    ...
    
    @code {
        protected override void OnInitialized()
        {
            var uri = new Uri(Navigation.Uri);
            var host = uri.Host;
            var domainParts = host.Split('.');
            if (domainParts.Length > 2)
            {
                // Return the first part as the subdomain
                UniqueId = domainParts[0];
            }
        }
    }
    
    

    Url:

    User's image

    Result:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

     

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.