如何在ASP.Net MVC中将访问者计算机的客户端IP地址保存到数据库?
问题描述:
我试图获得客户端机器的IP地址Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
它工作正常。如何在ASP.Net MVC中将访问者计算机的客户端IP地址保存到数据库?
获取IP在页面加载
public ActionResult AddCompany()
{
get_IP();
return View();
}
get_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地址值是显示。之后我点击保存按钮Ip
值不会保存到数据库中..
AddCompany
[HttpPost]
public ActionResult AddCompany(Company cmp)
{
using (DataContext entities = new DataContext())
{
entities.Company.Add(cmp);
entities.SaveChanges();
int id = cmp.CompanyID;
Session.Clear();
Response.Redirect("Index");
}
return View(cmp);
}
任何想法如何从数据库中保存的客户端IP Adrress ..
类文件
[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地址
http://tutorialgenius.blogspot.in/2010/09/aspnet-get-ipv4-address-even-if-user-is.html
答
我不知道HiddenFor因为我以前没有使用它。 我怎么躲领域是以前是这样的:
@Html.TextBoxFor(model => model.IPAddress, new { @class = "sr-only", @type = "hidden" })
您可以添加引导类SR-只,以确保它是隐藏的。 确保您检查您的浏览器检查器以查看隐藏字段是否有值。
如果发布,但仍然没有保存,然后尝试将其保存这样前分配Ip地址身份表单字段值:
cmp.IPAddress = Request.Form["IPAddress"];