Redirect web visitors by country using the .NET Framework in C# or VB.NET

There are times when it is useful to redirect a visitor to a different default web page based on the visitor’s country of origin. A practical use is to redirect the visitor to the web page with the language recognized by the visitor.

This article shows you how it can be done using the .NET component.

Let’s take a simple case study. XYZ Company is a multinational company with major customers in the United States and Japan. The company’s official website is developed in English and Japanese. The default page is in English and the visitor can switch to Japanese by changing the default language option. There is a potential problem when a Japanese visitor does not understand English and cannot navigate the website. So, let’s develop a simple solution to help XYZ Company to redirect all internet traffic from Japan country to Japanese language site. In the meantime, direct the rest of the traffic to the English site.

In this example, we use a fully functional IP2Location(TM) .NET component available at http://www.ip2location.net/download/IP2LocationDotNetComponent.ZIP to query the country by IP address of the visitor. First, install the IP2Location(TM) .NET component. The IP2Location(TM) .NET component will be installed on your local drive. Next, get the IP2Location.DLL .NET component and the sample database from the directory, ie. c:Program FilesIP2Location by default. You must add a reference to this component from your Visual Studio web project. A copy of this component will be copied to the /bin directory of the project. For the unregistered component, there is a random 5 second delay in one query out of ten.

Suppose the English web page is index_en.htm and the Japanese web page is index_jp.htm. We implement a simple default.asp script to detect the visitor’s country of origin. If the visitor is from Japan, redirect to index_jp.htm, otherwise index_en.htm. Easy? Here is the code and the comments serve as an explanation of default.asp.

Sample Codes in VB.NET Web Form

——————————

IP2Location Imports

Private Child Query (ByVal strIPAddress As String)

Dim oIPResult as new IP2Location.IPResult

try

If strIPAddress “” Then

IP2Location.Component.IPDatabasePath = “C:Program FilesIP2LocationDatabaseIP-COUNTRY.SAMPLE.BIN”

oIPResult = IP2Location.Component.IPQuery(strIPAddress)

Select case or IPResult.Status

“OK” box

If oIPResult.CountryShort = “JP” Then

‘The visitor is from Japan

‘ Redirect the URL to index_jp.htm

Response.Redirect(“index_jp.htm”)

Plus

‘The visitor is not from Japan

‘ Redirect the URL to index_en.htm

Response.Redirect(“index_en.htm”)

It will end if

“EMPTY_IP_ADDRESS” checkbox

Response.Write(“IP address cannot be blank”)

“INVALID_IP_ADDRESS” checkbox

Response.Write(“Invalid IP address”)

“MISSING_FILE” box

Response.Write(“Invalid database path”)

finish select

Plus

Response.Write(“IP address cannot be blank”)

It will end if

ex catch as exception

Reply.Write (for example, Message)

Finally

oIP Result = Nothing

end attempt

finish sub

Sample Codes in C# Webform

————————–

Using IP2Location;

private empty query (string strIPAddress)

{

IPResult oIPResult = new IP2Location.IPResult();

try

{

if (strIPAddress != “”)

{

IP2Location.Component.IPDatabasePath = “C:Program FilesIP2LocationDatabaseIP-COUNTRY.SAMPLE.BIN”;

oIPResult = IP2Location.Component.IPQuery(strIPAddress);

change(oIPResult.Status.ToString())

{

“OK” box:

if (oIPResult.CountryShort == “JP”) {

Response.Redirect(“index_jp.htm”)

} plus {

Response.Redirect(“index_en.htm”)

}

pause;

“EMPTY_IP_ADDRESS” frame:

Response.Write(“IP address cannot be blank.”);

pause;

“INVALID_IP_ADDRESS” frame:

Response.Write(“Invalid IP address.”);

pause;

“MISSING_FILE” box:

Response.Write(“Invalid database path.”);

pause;

}

}

plus

{

Response.Write(“IP address cannot be blank.”);

}

}

catch(ex exception)

{

Response.Write (eg Message);

}

Finally

{

oIPResult = null;

}

}

Build and upload this project to the website. All visitors will go through this evaluation before being redirected to an appropriate web page.

Leave a Reply

Your email address will not be published. Required fields are marked *