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

}

}

}
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.

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...

How to return Http Status Code from ASP.NET Web API Controller ?

There are times when you might want to return the Http Status code from an action method of a ASP.NE...