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:
xxxxxxxxxx
1
Uri myUri = new Uri("https://dirask.com/posts");
2
3
string host = myUri.Host;
4
5
Console.WriteLine(host); // Output: dirask.com
In this example, we create Uri
class object from given url
and use Host
property to get the domain name from it.
xxxxxxxxxx
1
using System;
2
3
public static class Program
4
{
5
public static void Main(string[] args)
6
{
7
string url = "https://dirask.com/posts";
8
9
Uri myUri = new Uri(url);
10
11
string host = myUri.Host;
12
13
Console.WriteLine(host);
14
}
15
}
Output:
xxxxxxxxxx
1
dirask.com