我试图在asp.net中获取 ServerVariables [" REMOTE_ADDR"] 。
这是我的旧代码(webapi 2):
data:
[
{
"scopeId": 101,
"scopeName": "FCM Attributes to be captured in CG1 OM",
"scopeDesc": "New Attributes to be captured in FCM",
"scopeIdentifier": "Q2FY16_FCM_Attributes_to_be_ca"
},
{
"scopeId": 104,
"scopeName": "PBI000000043281",
"scopeDesc": "OMLSS-OM-Production Bug- Workflow-Line Workflow purged even before Line is closed",
"scopeIdentifier": "FY16_OCT_MR_PBI000000043281"
},
{
"scopeId": 106,
"scopeName": "PBI000000049219",
"scopeDesc": "AS-T Inbound and re-process program issues",
"scopeIdentifier": "FY16_OCT_MR_PBI000000049219"
}
]
来自我所了解的他们已经改变了" Routing.RequestContext "到
"的 IHttpContextAccessor "在vNext版本中。
我如何用 IHttpContextAccessor 获得上述结果?
这给了我关于" ServerVariables "的错误部分:
private Logn GLog(System.Web.Routing.RequestContext requestContext)
{
Ln Log = new LogInformation();
Lg.IP = requestContext.HttpContext.Request.ServerVariables["REMOTE_ADDR"];
Lg.RemoteIP = requestContext.HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
............................
答案 0 :(得分:6)
您需要使用ConnectionInfo.RemoteIpAddress
代替:
private Logn GLog(IHttpContextAccessor contextAccessor)
{
Ln Log = new LogInformation();
Lg.IP = contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
// ...
}