Get domain name from URL

How to Get host domain from URL?

how to get domain name from UL

A URI (uniform resource identifier ) is a representation of a resource available to your application on the network. You can retrieve host of a url by using Request object or Uri.

Get domain name of a url

Using Request.Url

Dim host As String = Request.Url.Host.ToLower()
string host = Request.Url.Host.ToLower();

Using Uri


Uri myUri = new Uri(""); string host = myUri.Host;


Dim myUri As New Uri("") Dim host As String = myUri.Host

Or u can parse the url using regex

parse the url

How to parse a domain from URL?


using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressions; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string sURL = ""; Regex rg = new Regex("://(?<host>([a-z\\d][-a-z\\d]*[a-z\\d]\\.)*[a-z][-a-z\\d]+[a-z])"); if (rg.IsMatch(sURL)) { MessageBox.Show (rg.Match(sURL).Result("${host}")); } else { MessageBox.Show(string.Empty); } } } }

How to get domain part from URL


Imports System.Text.RegularExpressions Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sURL As String = "" Dim rg As New Regex("://(?<host>([a-z\d][-a-z\d]*[a-z\d]\.)*[a-z][-a-z\d]+[a-z])") If rg.IsMatch(sURL) Then MsgBox(rg.Match(sURL).Result("${host}")) Else MsgBox(String.Empty) End If End Sub End Class

Extract domain name from URL

A better way you could go without the need for regular expression or parsing is to use the below code.