How to get current domain name in ASP.NET

C#asp.net

C# Problem Overview


I want to get the current domain name in asp.net c#.

I am using this code.

string DomainName = HttpContext.Current.Request.Url.Host;

My URL is localhost:5858but it's returning only localhost.

Now, I am using my project in localhost. I want to get localhost:5858.

For another example, when I am using this domain

www.somedomainname.com

I want to get somedomainname.com

Please give me an idea how to get the current domain name.

C# Solutions


Solution 1 - C#

Try getting the “left part” of the url, like this:

string domainName = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);

This will give you either http://localhost:5858 or https://www.somedomainname.com whether you're on local or production. If you want to drop the www part, you should configure IIS to do so, but that's another topic.

Do note that the resulting URL will not have a trailing slash.

Solution 2 - C#

Using 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.

Note that 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 Request.Uri.Authority.

Furthermore, it is not valid to get somedomain.com from 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.

Solution 3 - C#

Here is a screenshot of Request.RequestUri and all its properties for everyone's reference.

enter image description here

Solution 4 - C#

You can try the following code :

 Request.Url.Host +
    (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port)

Solution 5 - C#

www.somedomain.com is the domain/host. The subdomain is an important part. www. is often used interchangeably with not having one, but that has to be set up as a rule (even if it's set by default) because they are not equivalent. Think of another subdomain, like mx.. That probably has a different target than www..

Given that, I'd advise not doing this sort of thing. That said, since you're asking I imagine you have a good reason.

Personally, I'd suggest special-casing www. for this.

string host = HttpContext.Current.Request.Url.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped);;

if (host.StartsWith("www."))
    return host.Substring(4);
else
    return host;

Otherwise, if you're really 100% sure that you want to chop off any subdomain, you'll need something a tad more complicated.

string host = ...;

int lastDot = host.LastIndexOf('.');

int secondToLastDot = host.Substring(0, lastDot).LastIndexOf('.');

if (secondToLastDot > -1)
    return host.Substring(secondToLastDot + 1);
else
    return host;

Getting the port is just like other people have said.

Solution 6 - C#

I use it like this in asp.net core 3.1

 var url =Request.Scheme+"://"+ Request.Host.Value;

Solution 7 - C#

HttpContext.Current.Request.Url.Host is returning the correct values. If you run it on www.somedomainname.com it will give you www.somedomainname.com. If you want to get the 5858 as well you need to use

HttpContext.Current.Request.Url.Port 

Solution 8 - C#

the Request.ServerVariables object works for me. I don't know of any reason not to use it.

ServerVariables["SERVER_NAME"] and ServerVariables["HTTP_URL"] should get what you're looking for

Solution 9 - C#

You can try the following code to get fully qualified domain name:

Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host

Solution 10 - C#

Here is a quick easy way to just get the name of the url.

            var urlHost = HttpContext.Current.Request.Url.Host;
           
            var xUrlHost = urlHost.Split('.');
            foreach(var thing in xUrlHost)
            {
                if(thing != "www" && thing != "com")
                {
                    urlHost = thing;
                }
            }

Solution 11 - C#

To get base URL in MVC even with subdomain www.somedomain.com/subdomain:

var url = $"{Request.Url.GetLeftPart(UriPartial.Authority)}{Url.Content("~/")}";

Solution 12 - C#

Try this:

@Request.Url.GetLeftPart(UriPartial.Authority)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJatin GadhiyaView Question on Stackoverflow
Solution 1 - C#Arturo Torres SánchezView Answer on Stackoverflow
Solution 2 - C#DaiView Answer on Stackoverflow
Solution 3 - C#ThiagoPXPView Answer on Stackoverflow
Solution 4 - C#Rajeev MehtaView Answer on Stackoverflow
Solution 5 - C#Matthew HaugenView Answer on Stackoverflow
Solution 6 - C#zoha_shView Answer on Stackoverflow
Solution 7 - C#artmView Answer on Stackoverflow
Solution 8 - C#PJ LeirerView Answer on Stackoverflow
Solution 9 - C#Yogesh PatelView Answer on Stackoverflow
Solution 10 - C#dstinebackView Answer on Stackoverflow
Solution 11 - C#Ivan-SanView Answer on Stackoverflow
Solution 12 - C#Hiren PatelView Answer on Stackoverflow