Monday 18 September 2017

How to get Client IP Address of Visitors Machine in ASP.Net MVC

How to get Client IP Address of Visitors Machine in ASP.Net MVC

Description:


In this example we explain that how to get client IP Address of the visitors machine in asp.net MVC.or how to get the IP Address of the visitors machine using MVC.or how can I get the IP Address of the our website visitor using ASP.NET MVC.

Here we get the client IP Address of the visitor and print it on Razor view in MVC.

Controller:

  public class EmployeeController : Controller
    {

        public ActionResult Index()
        {
            string clientIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(clientIPAddress))
            {
                clientIPAddress = Request.ServerVariables["REMOTE_ADDR"];
            }
            ViewBag.IPAddress = clientIPAddress;

            return View();
        }
    }

View:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>How to get Client IP Address of Visitors Machine in ASP.Net MVC</title>
</head>
<body>
    <h1>
        Your IP Address: @ViewBag.IPAddress
    </h1>
</body>
</html>


This entry was posted in :

0 comments:

Post a Comment