Search code examples
c#asp.net-core-mvc.net-8.0

How to get IP address in ASP.NET Core 8 MVC project


I am creating an ASP.NET Core 8 MVC project and trying to fetch IP address with this code:

var remoteIpAddress = HttpContext.Connection.RemoteIpAddress;

But, the code throws an exception:

Address = 'remoteIpAddress.Address' threw an exception of type System.Net.Sockets.SocketException

Please help me to solve this.

I tried to update the code but I am not able to fix it.


Solution

  • U can try this :

    HttpContext.Current.Request.UserHostAddress