How to get System IP address in ASP.NET MVC C#?

By 29th November 2018ASP.NET, C#, MVC

Getting client machine ip address in mvc.

you have to use System.Web.HttpContext context = System.Web.HttpContext.Current;
for current request.

public static string GetIPAddress()
            System.Web.HttpContext context = System.Web.HttpContext.Current;
            string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (!string.IsNullOrEmpty(ipAddress))
                string[] addresses = ipAddress.Split(',');
                if (addresses.Length != 0)
                    return addresses[0];

            return context.Request.ServerVariables["REMOTE_ADDR"];


Leave a Reply