在 Startup.cs 的配置中获取域或主机名和端口

Get the Domain or Host name and Port in Configure of Startup.cs

我在获取应用程序的 host/domain 名称和端口方面需要帮助。

到目前为止,这是我在 Startup.cs:

中的 Configure 方法中的示例代码
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var url = ""; // something that will get the current host/domain and port of the running applicaiton.

    RecurringJob.AddOrUpdate<ISomething>(i=>i.SomethingToExecute(url), Cron.Daily);
}

因为我需要将 url 字符串传递给我的 SomethingToExecute 方法

public class Something : ISomething 
{
    public void SomethingToExecute(string url){
        ... 
        Debug.WriteLine(url)
    }
}

根据此 Github Issue 和 ASP.NET 核心团队的回复:

It's not possible. You need to read this information directly from config. The server does not read the endpoint configuration until it's ready to start the application (after Startup). Even then, that information would be inaccurate much of the time due to reverse proxy setups like IIS, Nginx, docker, etc..

正如 TanvirArjel 已经提到的,这似乎是不可能的。但在您的情况下,您可以在代码主体中动态获取主机名。

(在 .Net Core 2.1 中测试)

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    services.AddSingleton<something>();
}

something.cs

public class something
{
    public string url;

    public string SomethingToExecute()
    {
        return url;
    }

}

控制器

[ApiController]
public class HomeController : ControllerBase
{

    private something _sth;

    public HomeController(something sth)
    {
        _sth = sth;
    }

    [HttpGet("foo")]
    public string foo()
    {

        _sth.url = Request.Scheme + "://" + Request.Host;
        return _sth.SomethingToExecute();

    }

}

这似乎是不可能的,但或者,如果您想使用 C# 代码调用 HTTP 请求或 Web API,您可以使用 HttpClient()

你可以做的是结合@TanvirArjel 所说的并使用 HttpClient() 调用 HTTP 请求。

  1. 将您的主机名存储在 appsettings.json
  2. 在你的SomethingToExecute()中使用这样的东西。

    private readonly IConfiguration configuration;
    public ISomething(IConfiguration configuration){
        this.configuration = configuration;
    }
    
    public void SomethingToExecute(){
        var httpClient = new HttpClient();
    
        var yourHostNameAndPort = configuration["NameFromYourAppSEttingsJsonFile"];
    
        httpClient.Get("" + yourHostNameAndPort + "...);
    }
    

我希望它对其他人也有帮助。

对于主机名 - 您可以使用 Dns.GetHostName() 在控制器中轻松获取它。 还有另一种方法是从 HttpRequestContext 中获取它,例如 Startup.cs => 配置服务 => services.AddHttpContextAccessor();

Controler => ctor => 使用 IHttpContextAccessor 然后在控制器方法中

HttpContextAccessor.HttpContext.Request.Host.Host HttpContextAccessor.HttpContext.Request.Host.Port

但是,HttpRequetsContext 信息不是很可信。

这里可能已经讨论了获取主机和端口的最佳方法:

这个替代选项可能有用,在您的应用程序设置中设置主机名。{Environment}。json并通过 IConfiguration 获取值:

private readonly IConfiguration _configuration;
public Startup(IConfiguration configuration){
    _configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
    var host = _configuration.GetSection("Host").Get<string>();
}