- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we get the client's IP address in ASP.NET MVC C#?
Every machine on a network has a unique identifier. Just as you would address a letter to send in the mail, computers use the unique identifier to send data to specific computers on a network. Most networks today, including all computers on the internet, use the TCP/IP protocol as the standard for how to communicate on the network. In the TCP/IP protocol, the unique identifier for a computer is called its IP address.
Using HttpRequest.UserHostAddress property
Example
using System.Web.Mvc; namespace DemoMvcApplication.Controllers{ public class HomeController : Controller{ public string Index(){ string ipAddress = Request.UserHostAddress; return ipAddress; } } }
If we want to fetch IP address outside the controller i.e. in a normal class, we can do like below.
using System.Web; namespace DemoMvcApplication.Helpers{ public static class DemoHelperClass{ public static string GetIPAddress(){ string ipAddress = HttpContext.Current.Request.UserHostAddress; return ipAddress; } } }
Example using ServerVariables
using System.Web.Mvc; namespace DemoMvcApplication.Controllers{ public class HomeController : Controller{ public string Index(){ string ipAddress = Request.ServerVariables["REMOTE_ADDR"]; return ipAddress; } } }
Output
Since we are running the application locally, the ip address for the localhost is ::1. The name localhost normally resolves to the IPv4 loopback address 127.0.0.1, and to the IPv6 loopback address ::1
- Related Articles
- How to get the Android Emulator's IP address?
- How to get the Android Emulator's IP address using Kotlin?
- How to get a Docker container's IP address from the host?
- C# program to find IP Address of the client
- How to get the ip address in C#?
- Java program to find IP Address of the client
- Python program to find the IP Address of the client
- Python program to remove leading 0's from an IP address
- Explain the MVC pattern in ASP.NET
- How to get the IP Address of local computer using C/C++?
- How to get the Android device's primary e-mail address?
- How to get a Docker Container IP address?
- How to get IP address settings using PowerShell?
- How can we test C# Asp.Net WebAPI?
- How to get the IP address of android device programmatically?

Advertisements