感谢!代码很详细,直接帮助解决了问题。
我最后的代码如下:
[C#] 纯文本查看 复制代码 [HttpPost("/RoadList/Query")]
public async Task<ResultData> Post([FromQuery] QueryData queryData)
{
var headers=HttpContext.Request.Headers;
string ip;
if (headers.ContainsKey("X-Forwarded-For"))
{
ip=System.Net.IPAddress.Parse(headers["X-Forwarded-For"].ToString().Split(',', StringSplitOptions.RemoveEmptyEntries)[0]).ToString();
}
else
{
ip=headers["X-Forwarded-For"].FirstOrDefault() ?? HttpContext.Connection.RemoteIpAddress!.ToString();
}
LogHelper.Info($"{ip}发起查询:{JsonConvert.SerializeObject(queryData)}");
} |