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

6

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

到目前为止,在我的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)
    }
}

也许这会对你有所帮助。https://dev59.com/_FcP5IYBdhLWcg3w_ex2#47051481 - Nilay Mehta
对我来说不起作用,兄弟。 - AppleCiderYummy
我认为这可能是一个XY问题。如果您编辑您的问题并解释为什么要这样做,可能会有所帮助。解决问题的方式可能有更好的解决方案。 - Simply Ged
5个回答

7
根据此Github问题和ASP.NET Core团队的回应:

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


但是在类内部而不是在 startup.cs 中获取 URL 是可能的吗? - AppleCiderYummy
像在我的 public void SomethingToExecute() 方法中获取 URL 一样? - AppleCiderYummy
我希望这是可能的。 - AppleCiderYummy
1
是的!我也是,因为应用程序还没有完全启动!最好将基本URL存储在appsettings.json文件中并从这里检索,或者使用硬编码。 - TanvirArjel
1
这就是为什么你应该将它存储在appsettings.json中。你可以在发布后轻松地从这里更改它。 - TanvirArjel
显示剩余4条评论

1
似乎不可能,但是如果你想使用C#代码调用HTTP请求或Web API,可以使用HttpClient()。你可以结合@TanvirArjel所说的内容,并使用HttpClient()来调用HTTP请求。
  1. Store your host name in appsettings.json
  2. In your SomethingToExecute() use something like this.

    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 + "...);
    }
    

我希望它也能帮助其他人。


0

这个替代方案可能很有用,可以在您的 appsettings.{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>();
}

0

关于主机名 - 您可以通过使用 Dns.GetHostName() 在控制器内轻松获取它。

还有另一种方法是从 HttpRequestContext 获取,例如 Startup.cs => ConfigureServices => services.AddHttpContextAccessor();

控制器 => 构造函数 => 使用 IHttpContextAccessor, 然后在控制器方法中使用。

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

然而,HttpRequetsContext信息并不是非常可靠。

可能获取主机和端口的最佳方法已经在这里讨论过了: 确定Kestrel绑定的端口


0
正如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();

    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接