[英]Domain alias - how to determine which one was used in .net core 3.1?
I have a web app using .net core 3.1 on IIS 10. I have my main domain set up and I have 3 alias domains that resolve to the main domain name.我有一个在 IIS 10 上使用 .net core 3.1 的 Web 应用程序。我设置了主域,并且有 3 个解析为主域名的别名域。 For my app I want to know which domain the user used to access the site.对于我的应用程序,我想知道用户用于访问该站点的域。
How could I determine that?我怎么能确定呢? Any help to get me pointed in the right direction would be great.任何让我指向正确方向的帮助都会很棒。
Thanks!谢谢!
If I understand this correctly, an alias domain is just an additional domain which points to the same server as your main domain, so you are not being redirected to the main domain?如果我理解正确,别名域只是一个附加域,它指向与您的主域相同的服务器,因此您不会被重定向到主域? Ie the address bar still shows the URL the user used to access your site.即地址栏仍然显示用户用来访问您的站点的 URL。 In that case, it should be as easy as reading the host name from the current URL.在这种情况下,它应该像从当前 URL 读取主机名一样简单。 You can grab that info via the IHttpContextAccessor
.您可以通过IHttpContextAccessor
获取该信息。
Step 1, register it as a service in the startup.cs file第一步,在startup.cs文件中将其注册为服务
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Step 2, make your controller depend on it, via DI:第 2 步,让你的控制器依赖它,通过 DI:
private readonly IHttpContextAccessor _httpContextAccessor;
public YourController(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
Step 3, read the info from the httpContextAccessor:第 3 步,从 httpContextAccessor 读取信息:
string domain = _httpContextAccessor.HttpContext.Request.Host.Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.