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

Published on February 19, 2013 by abundantcode

Filed under ASP.NET

Last modified February 19, 2013

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 11008 times

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

}

}

}

Leave a Comment


x

You might be Interested in these Posts

How to set default value for Html.TextBoxFor in ASP.NET MVC ?

You can set the default value for the Html.TextBoxFor extension controls in ASP.NET MVC by specifyin...

ASP.NET MVC and View State

The ASP.NET Web Forms includes the Server Controls which generally retains the state of the controls...

3 New C# 8 Features We Are Excited About

C# is rapidly approaching its third decade of life. Originally released in 2000, the language has gr...