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();

}

}

}
0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
Pin It

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>