10

I want to get host name/address in .net core application to create connection string dynamically in the ConfigureServices. I have tried so many ways. How can i get the Request detail in startup.cs files

jps
  • 20,041
  • 15
  • 75
  • 79
Chamara Nillushan
  • 151
  • 1
  • 1
  • 6
  • 4
    You cannot. Application startup happens only once, and long before any request has been received. In other words, there's no request to get information from. – Chris Pratt Apr 05 '19 at 18:21
  • 1
    By “host” do you mean the domain name of the web service? Or the name of the physical server? – John Wu Apr 05 '19 at 21:20
  • Possible duplicate of [Get the Domain or Host name and Port in Configure of Startup.cs](https://stackoverflow.com/questions/55369411/get-the-domain-or-host-name-and-port-in-configure-of-startup-cs) – TanvirArjel Apr 06 '19 at 03:12
  • @ChrisPratt you are correct. We can't use this in startup. I figure out another way full fill my requirement. Thanks a lot for saving my time. – Chamara Nillushan Apr 06 '19 at 09:41
  • @ChamaraNillushan could you please tell us what is your another way :) – Abdu Imam Mar 07 '20 at 16:48
  • Late to the party but this might be helpful - https://stackoverflow.com/a/50496810/4477493 – Braydie Dec 22 '22 at 09:53

1 Answers1

2

try this:

    public static string GetAddressIP()
    {
        return Dns.GetHostAddresses(Dns.GetHostName())
            .FirstOrDefault(ha => ha.AddressFamily == AddressFamily.InterNetwork)
            .ToString();
    }