My code:
protected void Page_Load(object sender, EventArgs e)
{
String userHost = Request.UserHostName
}
This is fetching the IP address alright, but now I want to know the country of the visitor and USE that country name to redirect the visitor to some webpage accordingly.
I came开发者_如何转开发 across this library but have no clue how to use it in the project. Actually I do, but I am not sure so I am asking.
http://ipaddressextensions.codeplex.com/
When I download from the above the ZIP folder has a DLL file and an XML file. Now, what do I do with these two? Like include in the project. Then what do I type in the code file?
Something like the following.
if (countryName=="France")
{
response.redirect("www.mysite.fr")
}
else
if(countryName=="India")
{
response.redirect("www.mysite.in")
}
and so on...
How do I go about it? Also do I really need to type SO many if
blocks for ALL the countries. How do I shorten this code?
To shorten your code put all the countries in a dictionary.
Dictionary<string,string> dict;
public void Init(){
dict = new Dictionary<string,string>();
dict["India"] = "www.mysite.in";
dict["France"] = "www.mysite.fr";
}
public string GetPage(string country){
string result = dict["Default"];
if(dict.ContainsKey(theKey)){
result = dict[theKey];
}
return result;
}
Just add the reference and then the "using statement" and the API is yours to use.
You could even alter the above to take in an IP address.
First add the reference and the below using
to your code.
using WorldDomination.Net;
public string GetPage(string ipAddress){
string result = null;
IPAddress ipAddress;
if (IPAddress.TryParse(userHostIpAddress, out ipAddress))
{
string fullNameKey= ipAddress.Country();
//Or you could use two letter code
//string twoLetterKey = ipAddress.Iso3166TwoLetterCode();
if(dict.ContainsKey(theKey)){
result = dict[fullNameKey];
}
}
else
{
result = dict["Default"];
}
return result;
}
You will need to add a reference to the downloaded library (dll) to your project. See http://msdn.microsoft.com/en-us/library/7314433t%28v=VS.90%29.aspx for details on adding references.
"What you type in the code file" will depend wholly on the library itself. If you're not sure how to implement the features of the library I suggest checking out the test project included in the source code repository hosted on the CodePlex page. It should show you what methods you'll need to call. With any luck, the class and method structure is self-explanatory.
If you prefer not to use if() { } else if() { }
blocks you can elect to use a switch statement instead.
switch(countryName) {
case "India":
// do something
break;
case "France":
// do something
break;
case "Japan":
// do something
break;
case "Germany":
// do something
break;
default:
// do something
break;
}
http://ipaddressextensions.codeplex.com/ site has given sample code for finding country. Once you found the country then compare it with your sub domains according to countries and then redirect it. ` 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
} `
精彩评论