How to retrieve the IP Address in ASP.NET MVC Web Application ?

If you want to retrieve the client’s IP Address in ASP.NET MVC , there are couple of ways that you can achieve this.

How to retrieve the IP Address in ASP.NET MVC Web Application ?

1. Use the Request.ServerVariables[“REMOTE_ADDR”] which will retrieve the IP address in ASP.NET

2. You could also use the Request.UserHostAddress to achieve the same.

Below is a sample controller sourecode demonstrating How to retrieve the IP Address in ASP.NET MVC Web Application ?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace MvcApplication1.Controllers

{

public class EmployeeController : Controller

{

//

// GET: /AbundantCode Employee Action/

public ActionResult Index()

{

string ipAddress = Request.ServerVariables["REMOTE_ADDR"];

string ipAddress2 = Request.UserHostAddress;

return View();

}

}

}