<p>I am wondering what the best way to obtain the current domain is in ASP.NET?</p>

<p>For instance:</p>

<p><a href="http://www.domainname.com/subdir/" rel="noreferrer">http://www.domainname.com/subdir/</a> should yield <a href="http://www.domainname.com" rel="noreferrer">http://www.domainname.com</a>
<a href="http://www.sub.domainname.com/subdir/" rel="noreferrer">http://www.sub.domainname.com/subdir/</a> should yield <a href="http://sub.domainname.com" rel="noreferrer">http://sub.domainname.com</a></p>

<p>As a guide, I should be able to add a url like "/Folder/Content/filename.html" (say as generated by Url.RouteUrl() in ASP.NET MVC) straight onto the URL and it should work.</p>

[{'answer_id': 61819, 'author': 'Matt Mitchell', 'author_id': 364, 'author_profile': 'https://Stackoverflow.com/users/364', 'pm_score': 5, 'selected': False, 'text': '<p>As per <a href="http://www.velocityreviews.com/forums/t89365-get-hostdomain-name.html" rel="noreferrer">this link</a> a good starting point is:</p>\n\n<pre><code>Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host \n</code></pre>\n\n<p>However, if the domain is <a href="http://www.domainname.com:500" rel="noreferrer">http://www.domainname.com:500</a> this will fail.</p>\n\n<p>Something like the following is tempting to resolve this:</p>\n\n<pre><code>int defaultPort = Request.IsSecureConnection ? 443 : 80;\nRequest.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host \n  + (Request.Url.Port != defaultPort ? ":" + Request.Url.Port : "");\n</code></pre>\n\n<p>However, port 80 and 443 will depend on configuration.</p>\n\n<p>As such, you should use <code>IsDefaultPort</code> as in the <a href="https://stackoverflow.com/a/2878350/364">Accepted Answer</a> above from Carlos Muñoz.</p>\n'}, {'answer_id': 61822, 'author': 'jwalkerjr', 'author_id': 689, 'author_profile': 'https://Stackoverflow.com/users/689', 'pm_score': -1, 'selected': False, 'text': '<p>How about:</p>\n\n<pre><code>String domain = "http://" + Request.Url.Host\n</code></pre>\n'}, {'answer_id': 64045, 'author': 'derek lawless', 'author_id': 400464, 'author_profile': 'https://Stackoverflow.com/users/400464', 'pm_score': 1, 'selected': False, 'text': '<p>How about:</p>\n\n<pre><code>NameValueCollection vars = HttpContext.Current.Request.ServerVariables;\nstring protocol = vars["SERVER_PORT_SECURE"] == "1" ? "https://" : "http://";\nstring domain = vars["SERVER_NAME"];\nstring port = vars["SERVER_PORT"];\n</code></pre>\n'}, {'answer_id': 2326934, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Another way:</p>\n\n<pre><code>\nstring domain;\nUri url = HttpContext.Current.Request.Url;\ndomain= url.AbsoluteUri.Replace(url.PathAndQuery, string.Empty);\n</code></pre>\n'}, {'answer_id': 2878350, 'author': 'Carlos Muñoz', 'author_id': 186133, 'author_profile': 'https://Stackoverflow.com/users/186133', 'pm_score': 9, 'selected': True, 'text': '<p>Same answer as MattMitchell\'s but with some modification.\nThis checks for the default port instead.</p>\n\n<blockquote>\n  <p>Edit: Updated syntax and using <code>Request.Url.Authority</code> as suggested </p>\n</blockquote>\n\n<pre><code>$"{Request.Url.Scheme}{System.Uri.SchemeDelimiter}{Request.Url.Authority}"\n</code></pre>\n'}, {'answer_id': 5845311, 'author': 'Thirlan', 'author_id': 590059, 'author_profile': 'https://Stackoverflow.com/users/590059', 'pm_score': 4, 'selected': False, 'text': '<p><strong>WARNING!</strong> To anyone who uses <strong>Current.Request</strong>.Url.Host. Understand that you are working based on the CURRENT REQUEST and that the current request will not ALWAYS be with your server and can sometimes be with other servers.</p>\n\n<p>So if you use this in something like, Application_BeginRequest() in Global.asax, then 99.9% of the time it will be fine, but 0.1% you might get something other than your own server\'s host name. </p>\n\n<p>A good example of this is something I discovered not long ago. My server tends to hit <a href="http://proxyjudge1.proxyfire.net/fastenv" rel="noreferrer">http://proxyjudge1.proxyfire.net/fastenv</a> from time to time. Application_BeginRequest() gladly handles this request so if you call Request.Url.Host when it\'s making this request you\'ll get back proxyjudge1.proxyfire.net. Some of you might be thinking "no duh" but worth noting because it was a very hard bug to notice since it only happened 0.1% of the time : P</p>\n\n<p>This bug has forced me to insert my domain host as a string in the config files.</p>\n'}, {'answer_id': 6727511, 'author': 'Korayem', 'author_id': 80434, 'author_profile': 'https://Stackoverflow.com/users/80434', 'pm_score': 4, 'selected': False, 'text': '<p>Why not use</p>\n\n<p><code>Request.Url.Authority</code></p>\n\n<p>It returns the whole domain AND the port.</p>\n\n<p>You still need to figure http or https</p>\n'}, {'answer_id': 8686506, 'author': 'izlence', 'author_id': 1123996, 'author_profile': 'https://Stackoverflow.com/users/1123996', 'pm_score': 5, 'selected': False, 'text': '<pre><code>Request.Url.GetLeftPart(UriPartial.Authority)\n</code></pre>\n\n<p>This is included scheme.</p>\n'}, {'answer_id': 29385136, 'author': 'Darren', 'author_id': 329367, 'author_profile': 'https://Stackoverflow.com/users/329367', 'pm_score': 0, 'selected': False, 'text': '<p>Using UriBuilder:</p>\n\n<pre><code>    var relativePath = ""; // or whatever-path-you-want\n    var uriBuilder = new UriBuilder\n    {\n        Host = Request.Url.Host,\n        Path = relativePath,\n        Scheme = Request.Url.Scheme\n    };\n\n    if (!Request.Url.IsDefaultPort)\n        uriBuilder.Port = Request.Url.Port;\n\n    var fullPathToUse = uriBuilder.ToString();\n</code></pre>\n'}, {'answer_id': 45777954, 'author': 'Ramin Bateni', 'author_id': 1474613, 'author_profile': 'https://Stackoverflow.com/users/1474613', 'pm_score': 2, 'selected': False, 'text': '<p><strong>Simple</strong> and <strong>short</strong> way (it support schema, domain and port):</p>\n<h2><strong>Use <code>Request.GetFullDomain()</code></strong></h2>\n<pre><code>// Add this class to your project\npublic static class HttpRequestExtensions{\n    public static string GetFullDomain(this HttpRequestBase request)\n    {\n        var uri= request?.UrlReferrer;\n        if (uri== null)\n            return string.Empty;\n        return uri.Scheme + Uri.SchemeDelimiter + uri.Authority;\n    }\n}\n\n// Now Use it like this:\nRequest.GetFullDomain();\n// Example output:    https://example.com:5031\n// Example output:    http://example.com:5031\n</code></pre>\n'}, {'answer_id': 63976140, 'author': 'Dastan Alybaev', 'author_id': 10049738, 'author_profile': 'https://Stackoverflow.com/users/10049738', 'pm_score': 1, 'selected': False, 'text': '<p>In <strong>Asp.Net Core 3.1</strong> if you want to get a full domain, here is what you need to do:</p>\n<p>Step 1: Define variable</p>\n<pre><code>private readonly IHttpContextAccessor _contextAccessor;\n</code></pre>\n<p>Step 2: DI into the constructor</p>\n<pre><code>public SomeClass(IHttpContextAccessor contextAccessor)\n{\n    _contextAccessor = contextAccessor;\n}\n</code></pre>\n<p>Step 3: Add this method in your class:</p>\n<pre><code>private string GenerateFullDomain()\n{\n    string domain = _contextAccessor.HttpContext.Request.Host.Value;\n    string scheme = _contextAccessor.HttpContext.Request.Scheme;\n    string delimiter = System.Uri.SchemeDelimiter;\n    string fullDomainToUse = scheme + delimiter + domain;\n    return fullDomainToUse;\n}\n//Examples of usage GenerateFullDomain() method:\n//https://example.com:5031\n//http://example.com:5031\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/61817', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/364/']