Asp.net mvc 如何在ASP.NETMVC中保存访问者机器的数据库客户端IP地址?
我正在尝试获取客户端计算机的IP地址Asp.net mvc 如何在ASP.NETMVC中保存访问者机器的数据库客户端IP地址?,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-3,ip-address,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc 3,Ip Address,我正在尝试获取客户端计算机的IP地址Request.ServerVariables[“HTTP_X_FORWARDED_FOR”]工作正常 在页面加载时获取IP public ActionResult AddCompany() { get_IP(); return View(); } 获取IP代码 public void get_IP() { string ipAddress
Request.ServerVariables[“HTTP_X_FORWARDED_FOR”]代码>工作正常
在页面加载时获取IP
public ActionResult AddCompany()
{
get_IP();
return View();
}
获取IP代码
public void get_IP()
{
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
ViewBag.IPAddress = ipAddress;
}
但当用户注册时,表单得到的IP地址值也保存在数据库中
Cs.html文件
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1 class="page-header">Add Company</h1>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.IPAddress)
<div class="form-group">
@Html.LabelFor(model => model.CompanyName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CompanyName)
@Html.ValidationMessageFor(model => model.CompanyName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ShortName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ShortName)
@Html.ValidationMessageFor(model => model.ShortName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Country, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*@Html.EditorFor(model => model.Country)*@
@Html.DropDownList("Country", null, "---Select Country----")
@Html.ValidationMessageFor(model => model.Country)
</div>
</div>
<div class="form-group">
<b>State: </b>
<select id="state"></select><br />
</div>
<div>
<b>City: </b>
<select id="city"></select><br />
</div>
<div class="form-group">
@Html.LabelFor(model => model.MobileNo, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MobileNo)
@Html.ValidationMessageFor(model => model.MobileNo)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</div>
知道如何从数据库中保存客户端IP地址吗
类文件
[Table("MySecondMVCDemo")]
public class Company
{
[Key]
public int CompanyID { get; set; }
[Required(ErrorMessage = "Please Enter Company Name")]
[Display(Name = "CompanyName")]
public string CompanyName { get; set; }
[Required(ErrorMessage = "Please Enter Short Name")]
[Display(Name = "ShortName")]
public string ShortName { get; set; }
[Required(ErrorMessage = "Please Enter Email Address")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "Please Enter Email Address")]
[Display(Name = "Country")]
public string Country { get; set; }
[Required(ErrorMessage = "Please Enter Mobile No")]
[Display(Name = "MobileNo")]
public string MobileNo { get; set; }
public Int32? IPAddress { get; set; }
}
将IP地址存储为字符串而不是Int32
请使用此处的代码获取IP地址
我不知道HiddenFor,因为我以前没有使用过它。
我以前是如何隐藏字段的:
@Html.TextBoxFor(model => model.IPAddress, new { @class = "sr-only", @type = "hidden" })
cmp.IPAddress = Request.Form["IPAddress"];
您可以添加引导类sr only,以确保它是隐藏的。
确保检查浏览器检查器以查看隐藏字段是否有值
如果已过账但仍未保存,请在保存前尝试将IpAddress标识分配给表单字段值,如下所示:
@Html.TextBoxFor(model => model.IPAddress, new { @class = "sr-only", @type = "hidden" })
cmp.IPAddress = Request.Form["IPAddress"];