I want to get the current domain name in asp.net c#.
I am using this code.
string DomainName = HttpContext.Current.Request.Url.Host;
Request.Url.Host is appropriate - it's how you retrieve the value of the HTTP
Host: header, which specifies which hostname (domain name) the UA (browser) wants, as the Resource-path part of the HTTP request does not include the hostname.
localhost:5858 is not a domain name, it is an endpoint specifier, also known as an "authority", which includes the hostname and TCP port number. This is retrieved by accessing
Furthermore, it is not valid to get
www.somedomain.com because a webserver could be configured to serve a different site for
www.somedomain.com compared to
somedomain.com, however if you are sure this is valid in your case then you'll need to manually parse the hostname, though using
String.Split('.') works in a pinch.
Note that webserver (IIS) configuration is distinct from ASP.NET's configuration, and that ASP.NET is actually completely ignorant of the HTTP binding configuration of the websites and web-applications that it runs under. The fact that both IIS and ASP.NET share the same configuration files (
web.config) is a red-herring.