2013-01-23 79 views
0

我正在生成運行時間<a>鏈接。要完成鏈接,請使用波紋管代碼:當用戶使用IP地址打開網站時如何獲取IP地址?

string appPath = protocol + System.Web.HttpContext.Current.Request.ServerVariables["HTTP_HOST"] + System.Web.HttpContext.Current.Request.ApplicationPath;

但是,當用戶嘗試從此處打開網站時:http://123.123.123.123/testApp我的鏈接是使用http://myservername.com/testApp創建的。

我想要用戶輸入的地址。

如果從http://123.123.123.123/testApp用戶打開打開網站上的鏈接應該是

http://123.123.123.123/testApp/Default.aspx

以及如果從http://myservername.com/testApp用戶打開網站上的鏈接應該是

http://myservername.com/testApp/Default.aspx

+0

附註:只有當您的網站是唯一指定IP的網站時纔有意義。 –

+0

我沒有向用戶顯示Live IP。所以我給用戶提供了假IP。他們通過IP地址或站點名稱訪問網站。 –

+0

爲什麼不只是提供一個相對的鏈接,而不是試圖構建一個絕對的鏈接? –

回答

0

使用REMOTE_ADDR服務器變量

string appPath = protocol + 
System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] + 
System.Web.HttpContext.Current.Request.ApplicationPath; 
+0

我想你可以通過其他方式來尋找LOCAL_ADDR變量 - 用戶使用DNS名稱或直接IP輸入網站的地址。 –

0

HTTP_HOST or SERVER_NAME之一應該爲您提供有關輸入您的網站的用戶信息。使用Http調試器檢查瀏覽器實際發送的IP地址以確保檢查正確的值是個好主意。

`HttpContext.Current.Request.ServerVariables("HTTP_HOST");` 
0

使用此功能可獲得用戶的IPAddress
但是您可能無法獲得實際的IP。有很多關注如firewall等,這可能會暴露相同的網絡上的所有計算機ip

public static string GetMachineName(HttpRequest moRequest) 
    { 
     return moRequest.ServerVariables["HTTP_X_FORWARDED_FOR"] != null || 
       moRequest.ServerVariables["HTTP_CLIENT_IP"] != null 
        ? moRequest.ServerVariables["HTTP_X_FORWARDED_FOR"] 
        : moRequest.ServerVariables["REMOTE_ADDR"]; 
    }