我对 asp.net mvc 堆栈完全陌生,我想知道 Request.Header 对象发生了什么?
基本上我想做的是拉出设备(PC)IP地址,但我无法检索所需的输出。我也尝试过 Request.ServerVariables 对象,但结果始终保持为 NULL。
我正在使用 asp.net MVC。此功能是否需要任何更改:
public static string GetIP()
{
string functionReturnValue = null;
//Gets IP of actual device versus the proxy (WAP Gateway)
functionReturnValue = HttpContext.Current.Request.Headers["X-FORWARDED-FOR"]; //functionReturnValue = null
if (string.IsNullOrEmpty(functionReturnValue))
{
functionReturnValue = HttpContext.Current.Request.Headers["X-Forwarded-For"];//functionReturnValue = null
if (string.IsNullOrEmpty(functionReturnValue))
{
//If not using a proxy then get the device IP
// GetIP = Context.Request.ServerVariables("REMOTE_ADDR")
if (string.IsNullOrEmpty(functionReturnValue))
{
//If not using a proxy then get the device IP
functionReturnValue = HttpContext.Current.Request.Headers["X-CLIENT-IP"];//functionReturnValue = …Run Code Online (Sandbox Code Playgroud)