問題和我的問題如下: -錯誤使用Global.asax文件重定向用戶時::頁面的重定向不,我面對的正確
我只本地主機上運行的代碼,有沒有免費服務或者我可以在線運行這個示例webite的東西? 否則以下代碼返回的IP地址始終爲127.0.0.1。我如何測試我的代碼?目前我給了IP地址的靜態值。
HTTP_X_FORWARDED_FOR總是返回空值。我讀過只有HTTP_X_FORWARDED_FOR包含用戶的真實IP地址。
REMOTE_ADDR是否包含用戶的真實IP地址,或者其中的IP也可能被欺騙?我只想知道用戶所在的國家,所以我可以根據ISO代碼重定向用戶。
我面臨的一個主要問題是我無法在此Global.asax文件中看到MasterPageFile屬性。
我想是這樣的: -
如果ISO代碼爲「FR」然後加載該網站的主人是FranceMaster.master 否則,如果ISO代碼爲「GB」那麼將使用BritainMaster.master文件。
在這個文件的哪個部分設置主頁面?
所以基本上我想學習如何根據ISO代碼重定向用戶並設置主頁。我給了硬編碼的IP地址,如122.87.234.1,然後它給了我這個錯誤,不能正確重定向。我搜索了一下,發現response.redirect 請求被無限期地調用。如果以下方式不是如何重定向用戶,那麼還有其他方法嗎?
我相信這種東西已經實施了太多次了。有人可以幫我解決這個問題嗎?我會很感激。謝謝。
下面是我寫在Global.asax文件的代碼: -
<%@ Application Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="WorldDomination.Net" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
void Application_BeginRequest(object sender, EventArgs e)
{
string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
{
string[] ipRange = ip.Split(',');
int le = ipRange.Length - 1;
string trueIP = ipRange[le];
ip = trueIP;
}
else
{
ip = Request.ServerVariables["REMOTE_ADDR"];
}
// string userHostIpAddress = "203.1.2.3";
string userHostIpAddress = ip;
IPAddress ipAddress;
if (IPAddress.TryParse(userHostIpAddress, out ipAddress))
{
string country = ipAddress.Country(); // return value: UNITED STATES
string iso3166TwoLetterCode = ipAddress.Iso3166TwoLetterCode(); // return value: US
}
if (ipAddress.Iso3166TwoLetterCode() != null)
{
if (ipAddress.Iso3166TwoLetterCode().ToLower() == "fr")
{
Response.Redirect("www.mysite.fr");
}
if (ipAddress.Iso3166TwoLetterCode().ToLower() == "ja")
{
Response.Redirect("www.mysite.com/BlockedAccess.aspx");
}
}
else
{
Response.Redirect("www.mysite.com");
}
}
</script>
編輯: -
@克里斯::感謝。我有點困惑。如果IP地址始終可能被欺騙,我怎樣才能獲得用戶的國家?我的意思是如何確保我提取的用戶位置正確?另外,實際的應用程序項目上有100多頁,我不得不努力工作。目前,我只是通過一個頁面製作示例應用程序來嘗試它。現在,在所有頁面中設置masterpagefile屬性將是一項非常重要的任務。有沒有辦法讓它在一個地方完成?這個網絡應用程序中的字面意思就是這些。請回復。謝謝。
這與您發佈的此問題是相同的,或者至少非常非常相似:[如何使用Global.asax文件重定向用戶](http://stackoverflow.com/questions/5083794/how-使用全球asax文件重定向用戶) – TLS 2011-02-24 23:09:36