Net-informations.com
SiteMap  | About    

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

C#

  string host = Request.Url.Host.ToLower();

vb.net

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

Using Uri

C#

Uri myUri = new Uri("http://forums.asp.net/t/1110512.aspx?"); string host = myUri.Host;

VB.Net

Dim myUri As New Uri("http://forums.asp.net/t/1110512.aspx?") Dim host As String = myUri.Host

Or u can parse the url using regex

parse the url

How to parse a domain from URL?

C#

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 = "http://forums.asp.net/t/1110512.aspx?"; 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

VB.Net

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 = "http://forums.asp.net/t/1110512.aspx?" 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

C#

  Request.ServerVariables["HTTP_HOST"]

vb.net

  Request.ServerVariables("HTTP_POST")









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.