[英]How do I get the current subdomain within .Net Core middleware?
How can I get the current subdomain for the current request (in a middleware component) in asp.net 5.如何在 asp.net 5 中获取当前请求(在中间件组件中)的当前子域。
I previously used the code below and looking for something similar.我以前使用过下面的代码并寻找类似的东西。
public static string GetSubDomain()
{
string subDomain = String.Empty;
if (HttpContext.Current.Request.Url.HostNameType == UriHostNameType.Dns)
{
subDomain = Regex.Replace(HttpContext.Current.Request.Url.Host, "((.*)(\\..*){2})|(.*)", "$2").Trim().ToLower();
}
if (subDomain == String.Empty)
{
subDomain = HttpContext.Current.Request.Headers["Host"].Split('.')[0];
}
return subDomain.Trim().ToLower();
}
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.然后用 split 或任何你喜欢的东西来操纵这个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.