在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
// ASP.NET Core 2.1的注入方式 //services.AddHttpContextAccessor(); //services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>(); // 注入 services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 2、控制器里获取IPprivate readonly IHttpContextAccessor _accessor; public HomeController(IHttpContextAccessor accessor) { _accessor = accessor; } public IActionResult Index() { // 获取客户端的IP string ip = _accessor.HttpContext.Connection.RemoteIpAddress.ToString(); return View(); } 3、页面里获取IP@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor @{ ViewData["Title"] = "Privacy Policy"; } <h1>@ViewData["Title"]</h1> <p>客户端IP:@HttpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString().</p>
|
请发表评论