by
Neon Quach
23. February 2011 23:20
In this blog I share my snippets code which can get your current domain name using C# in asp.net
We have three ways to complete this task
using System;
using System.Collections.Specialized;
using System.Web;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 1st
Response.Write(Request.Url.Scheme + Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.Port != 80 ? ":" + Request.Url.Port : ""));
Response.Write("</br>");
NameValueCollection variables = HttpContext.Current.Request.ServerVariables;
string protocol = (variables["SERVER_PORT_SECURE"] == "1") ? "https://" : "http://"; string domain = variables["SERVER_NAME"];
string port = variables["SERVER_PORT"];
// 2nd
Response.Write(protocol + domain + port);
Response.Write("</br>");
// 3rd
string url = HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.AbsolutePath, "");
Response.Write(url);
}
}
Hope this help!