posted 11/26/2010 by Raghav Khunger
Today a person asked me on how to get the domain name from URL in ASP.NET, I decided to write a blog on the solution which I gave to him. Let's say you have a URL say "http://www.mydomain.com/test/default.aspx" and you want to extract "www.mydomain.com" from it. You can make use of UriInstance.Host property in order to get it. It brings the host component of the current Uri instance. Below is the sample code:
using System; public partial class default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string url = "http://www.mydomain.com/test/default.aspx"; Uri uri = new Uri(url); string domain = uri.Host; Response.Write(string.Format("Domain: {0}", domain)); } }
Do let me know your feedback, comments.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18