tags:

views:

62

answers:

2

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 lib but have no clue how to use it in the project..actually I do but I am not sure so asking....plz someone guide me with this

http://ipaddressextensions.codeplex.com/

when I d/l from 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...

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 ??

Plz help me out..thnx

+1  A: 

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;
}
Nathan Taylor
ok added the dll ..what's that xml for ? do I need to include that too in the project ?
Serenity
Also..that dll file is stored on my desktop..if I add the reference in my project and then delete the dll from desktop..will it give error saying dll missing ?
Serenity
@happysoul The XML file contains the library documentation. You don't need to reference the XML itself, but if you keep it in the same folder as the DLL IntelliSense will give you documentation for the class.
Nathan Taylor
@happysoul The DLL file is automatically copied to the "bin" directory of the referencing project. So long as you don't delete it from the bin directory you should be fine- copy the XML file there too if Visual Studio failed to.
Nathan Taylor
It saves the original location. SO if you were to delete the file on your desktop, then clean you will lose the dll. You should move the dll to some type of folder and include it in your solution.
Nix
+3  A: 

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 your 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;
}
Nix
And to shorten it evenmore use initializer syntax. new Dictionary<string,string>{{contry1,url1},...,{countryn,urln}};
Rune FS