Languages
[Edit]
EN

C# / .NET - get domain name from given url

0 points
Created by:
Theodora-Battle
528

In this article, we would like to show you how to get domain name from given url in C#.

Quick solution:

Uri myUri = new Uri("https://dirask.com/posts");

string host = myUri.Host;

Console.WriteLine(host); // Output: dirask.com

 

Practical example

In this example, we create Uri class object from given url and use Host property to get the domain name from it.

using System;

public static class Program
{
    public static void Main(string[] args)
    {
        string url = "https://dirask.com/posts";

        Uri myUri = new Uri(url);

        string host = myUri.Host;

        Console.WriteLine(host);
    }
}

Output:

dirask.com

References

  1. Uri Class (System) | Microsoft Docs
  2. Uri.Host Property (System) | Microsoft Docs

Alternative titles

  1. C# / .NET - get host domain from given url
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join