标签:pre dht 客户端 地址 ipaddr 方式 tostring add context
// ASP.NET Core 2.1的注入方式 //services.AddHttpContextAccessor(); //services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>(); // 注入 services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
private readonly IHttpContextAccessor _accessor; public HomeController(IHttpContextAccessor accessor) { _accessor = accessor; } public IActionResult Index() { // 获取客户端的IP string ip = _accessor.HttpContext.Connection.RemoteIpAddress.ToString(); return View(); }
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor @{ ViewData["Title"] = "Privacy Policy"; } <h1>@ViewData["Title"]</h1> <p>客户端IP:@HttpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString().</p>
标签:pre dht 客户端 地址 ipaddr 方式 tostring add context
原文地址:https://www.cnblogs.com/zhouxiaoyun/p/10765509.html