How do I get the current subdomain within .Net Core middleware? How do I get the current subdomain within .Net Core middleware? asp.net asp.net

How do I get the current subdomain within .Net Core middleware?


I have managed to work out my own answer in the meantime...comments appreciated.

private static string GetSubDomain(HttpContext httpContext)        {            var subDomain = string.Empty;            var host = httpContext.Request.Host.Host;            if (!string.IsNullOrWhiteSpace(host))            {                subDomain = host.Split('.')[0];            }            return subDomain.Trim().ToLower();        }


If you're using Cross-Domain, the best option is to use the Origin from Request-Header.Something like:

Request.Headers.TryGetValue("Origin",  out var origin);

and then manipulate the value with split or whatever you like.