2010-09-11 51 views
0

好的,這裏是...您可以獲取用戶的IP地址,並根據他/她的國家/地區將用戶重定向到特定的網頁。現在,你如何動態改變主頁面?這是怎麼了將用戶重定向: -初學ASP.NET C#關於動態更改母版頁的問題

Geolocation error with IP address 127.0.0.1

它不象用戶點擊一些按鈕或東西,然後你改變了母版頁。我希望在用戶重定向時更改它,那麼我該如何去做呢?

public partial class testClass: System.Web.UI.MasterPage 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (redirected to site1) 
     { 
       Use this master page1 
     } 
     else 
      if (redirected to site2) 
      { 
       Use this master page2 
      } 
    } 
} 

那麼,如何檢查用戶被重定向到的SITE?現在如何應用特定的母版頁,現在用戶已被重定向?

我只是需要一個想法如何去做。

[編輯]請檢查下面的代碼塊。如何獲取用戶重定向到的URL?實際上我只需要「iso3166TwoLetterCode」變量的值(請參閱我之前問題的鏈接),並基於此將更改母版頁。我無法弄清楚如何獲取這個值,或者甚至在我的testClass中使用那個類(有這個變量)。

protected void Page_PreInit(object sender, EventArgs e) 
    { 
     if (user Has been redirected to www.site.in) 
     { 
      this.MasterPageFile = "master1.master"; 
     } 

     if (user Has been redirected to www.site.fr) 
     { 
      this.MasterPageFile = "master2.master"; 
     } 
    } 

回答

1

要了解這個國家的兩個字母的代碼,做什麼,從這個http://ipaddressextensions.codeplex.com/示例代碼:

using System.Net; 
using WorldDomination.Net; 

string userHostIpAddress = "203.1.2.3"; 
IPAddress ipAddress; 
if (IPAddress.TryParse(userHostIpAddress, out ipAddress)) 
{ 
    string country = ipAddress.Country(); // return value: UNITED STATES 
    string iso3166TwoLetterCode = ipAddress.Iso3166TwoLetterCode(); // return value: US 
} 

然後你可以嘗試這樣的事情來改變主:

protected void Page_PreInit(object sender, EventArgs e) 
{ 
    this.MasterPageFile = "NewMasterSite.master"; 
} 
+0

你能否檢查我剛添加的附加細節? thnx – Serenity 2010-09-11 11:13:56

+0

我以爲你已經實現了這一點。 – Philipp 2010-09-11 12:03:30

+0

想出瞭如何從URL.got幫助這裏獲取代碼http://stackoverflow.com/questions/3698196/how-do-i-check-for-a--a-urls-top-level-domain-in- ASP-網-C – Serenity 2010-09-17 07:10:10

0

這聽起來像你不是重定向到另一個站點,只需將它們發送到一個頁面中包含「語言= EN」查詢字符串。如果是這樣,那麼你需要使用Request.QueryString來獲取它並將其附加到基本主頁面名稱。

+0

OKK ..所以如果它是一個網頁..我喜歡取URL(eg.www.mysite.in),然後使用「Request.QueryString」..檢查它後,我該如何更改母版頁? – Serenity 2010-09-11 10:58:20

+0

根據你的例子,你根本沒有使用查詢字符串。相反,您可能在虛擬服務器上設置了多個子域。在這種情況下,請嘗試'Request.Uri'。如果這不起作用,'Request.ServerVariables [「Host」]'會。 – 2010-09-11 11:35:19

+0

允許我糾正自己:HTTP頭被稱爲「主機」,但您必須通過「HTTP_HOST」在此處引用它。或者,我認爲「網址」會做你想做的。 – 2010-09-11 11:43:00