In this article I am going to explain how to get IP address of users using asp.net mvc.
I want to get the IP address of visitors.
Add an empty controller to project. Create an actionresult to get IP address.
Complete code of controller
public class EmployeeController : Controller
private DemoEntities db = new DemoEntities();
// GET: /Employee/
public ActionResult Index()
public ActionResult GetIPAddress()
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];
ViewBag.ipaddress = ipaddress;
Add view for GetIPAddress action.
Complete source of view
ViewBag.Title = "ASP.NET MVC : Get IP address of users";
IP address of user is : @ViewBag.ipaddress
*Note: This will show IP address ::1 when you run this on localhost but when you deploy the application and upload it on server, it will be work perfectly.
What do you think about this article?
If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.