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

App*_*mmy 6 c# asp.net asp.net-core asp.net-core-2.0 asp.net-core-2.1

我需要帮助来获取我的应用程序的主机/域名和端口。

到目前为止,这是我的Configure方法中的示例代码Startup.cs

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);
}
Run Code Online (Sandbox Code Playgroud)

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

public class Something : ISomething 
{
    public void SomethingToExecute(string url){
        ... 
        Debug.WriteLine(url)
    }
}
Run Code Online (Sandbox Code Playgroud)

Tan*_*jel 7

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

这是不可能的。您需要直接从 config 中读取此信息。服务器在准备好启动应用程序之前(启动后)不会读取端点配置。即使如此,由于 IIS、Nginx、docker 等反向代理设置,该信息在很多时候仍然不准确。