Return to Snippet

Revision: 43173
at March 18, 2011 07:33 by jasonseney


Initial Code
public static string ServerHostName()
{
	string port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
	if (port == null || port == "80" || port == "443")
	{
		port = "";
	}
	else
	{
		port = ":" + port;
	}

	string protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
	if (protocol == null || protocol == "0")
	{
		protocol = "http://";
	}
	else
	{
		protocol = "https://";
	}

	return protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port;
}

Initial URL

                                

Initial Description
Will return the host name that the site is running on. Example:

`http://mysite.com` or `http://myspecialsite.com:102`

Initial Title
Get the Host Name in ASP.NET C#

Initial Tags
aspnet

Initial Language
C#