Iam using Mvc4, I want to get the users ip address of who are using my site. Based on this link I can get user host address, But i want to get exact user ip address
Depending on the place you are.
Controller:
ControllerContext.HttpContext.Request.UserHostAddress;
Razor View:
@Request.UserHostAddress
Model:
Do not use System.Web.HttpContext since it's very hard, if not impossible to unit test. Instead, pass the value in through your controller.
Html Helper:
public static MvcHtmlString Ip(this HtmlHelper html)
{
string html = html.ViewContext.RequestContext.HttpContext.Request.UserHostAddress;
return MvcHtmlString.Create(html);
}
Keep in mind, due to simplicity of the examples, I didn't do any null-checking.
Try using following code
System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With