Redirect Web Visitors By Country Using .NET Framework in C# or VB.NET

There are occasions when it turns out to be useful to redirect a customer to other default internet web page according to the customer’s nation of foundation. One sensible utilization is to redirect customer to internet web page with the language identified by means of the customer.

This article displays you ways by means of the usage of .NET part, it may be accomplished.

Let us take a easy case find out about. Company XYZ is multi-national corporate with main shoppers from United States and Japan. The corporate reputable web page is evolved in each English and Japanese languages. The default web page is in English language and customer can transfer to Japanese by means of converting the default language possibility. There exists a possible drawback when a Japanese customer does now not perceive English and it might now not navigate the internet website online. So allow us to expand a easy method to lend a hand Company XYZ redirecting all Internet visitors from nation Japan to the Japanese language website online. Meanwhile it drives the remainder visitors to English website online.

In this case, we use an absolutely purposeful IP2Location(TM) .NET part to be had at http://www.ip2location.net/download/IP2LocationDotNetComponent.ZIP to question nation by means of customer’s IP deal with. Firstly, set up the IP2Location(TM) .NET part. The IP2Location(TM) .NET part can be put in on your native pressure. Next, get the IP2Location.DLL .NET part and pattern database from the listing, ie. c:Program FilesIP2Location by means of default. You want to upload a connection with this part out of your Visual Studio internet challenge. A replica of this part can be copied into /bin listing beneath the challenge. For unregistered part, there’s a random 5-second lengthen in a single out of ten queries.

Let’s suppose the English internet web page as index_en.htm and Japanese internet web page as index_jp.htm. We put in force a easy script default.asp to discover customer’s nation of foundation. If the customer is from Japan, then redirect him/her to index_jp.htm, in a different way index_en.htm. Simple? Here is the code and the feedback function clarification default.asp.

Sample Codes in VB.NET Webform

——————————

Imports IP2Location

Private Sub 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 oIPResult.Status

Case “OK”

If oIPResult.CountryQuick = “JP” Then

‘ Visitor is from Japan

‘ Redirect the URL to index_jp.htm

Response.Redirect(“index_jp.htm”)

Else

‘ Visitor isn’t from Japan

‘ Redirect the URL to index_en.htm

Response.Redirect(“index_en.htm”)

End If

Case “EMPTY_IP_ADDRESS”

Response.Write(“IP Address can’t be clean.”)

Case “INVALID_IP_ADDRESS”

Response.Write(“Invalid IP Address.”)

Case “MISSING_FILE”

Response.Write(“Invalid Database Path.”)

End Select

Else

Response.Write(“IP Address can’t be clean.”)

End If

Catch ex As Exception

Response.Write(ex.Message)

Finally

oIPResult = Nothing

End Try

End Sub

Sample Codes in C# Webform

————————–

Using IP2Location;

personal void Query(string strIPAddress)

{

IPResult oIPResult = new IP2Location.IPResult();

take a look at

{

if (strIPAddress != “”)

{

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

oIPResult = IP2Location.Component.IPQuery(strIPAddress);

transfer(oIPResult.Status.ToString())

{

case “OK”:

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

Response.Redirect(“index_jp.htm”)

} else {

Response.Redirect(“index_en.htm”)

}

smash;

case “EMPTY_IP_ADDRESS”:

Response.Write(“IP Address can’t be clean.”);

smash;

case “INVALID_IP_ADDRESS”:

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

smash;

case “MISSING_FILE”:

Response.Write(“Invalid Database Path.”);

smash;

}

}

else

{

Response.Write(“IP Address can’t be clean.”);

}

}

catch(Exception ex)

{

Response.Write(ex.Message);

}

in any case

{

oIPResult = null;

}

}

Compile and add this challenge to the internet website online. All guests will undergo this screening earlier than redirect to a suitable internet web page.