在我的startup.cs配置中,我需要获取域名。 我通过在IConfig中注入IHttpContextAccessor来做到这一点:
public void ConfigureServices(IServiceCollection services)
{
[...]
services.AddHttpContextAccessor();
[...]
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IHttpContextAccessor httpContext, DBContext dbContext)
{
[...]
var host = httpContext.HttpContext.Request.Host.Host;
var country = dbContext.Countries.FirstOrDefault(x => x.Domain == host);
/*
var cultureInfo = new CultureInfo(country.CultureInfo);
cultureInfo.NumberFormat.CurrencySymbol = country.CurrencySymbol;
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
*/
[...]
app.UseMvc();
}
如果我删除了var host = httpContext.HttpContext.Request.Host.Host;
,则应用程序可以正常运行,但是如果存在,则该应用程序的构建会在VS Mac中中断(dotnet崩溃)。
HttpContext
似乎为空,因为var host = httpContext.HttpContext?.Request.Host.Host;
不会使应用程序崩溃。
我想念什么?