在Asp.Net Core 3.1中,如果您想获得完整的域,您需要执行以下操作:
第 1 步:定义变量
private readonly IHttpContextAccessor _contextAccessor;
第 2 步:DI 进入构造函数
public SomeClass(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
第 3 步:在您的类中添加此方法:
private string GenerateFullDomain()
{
string domain = _contextAccessor.HttpContext.Request.Host.Value;
string scheme = _contextAccessor.HttpContext.Request.Scheme;
string delimiter = System.Uri.SchemeDelimiter;
string fullDomainToUse = scheme + delimiter + domain;
return fullDomainToUse;
}
//Examples of usage GenerateFullDomain() method:
//https://example.com:5031
//http://example.com:5031