EN
C# / .NET - get domain name from given url
0
points
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