Use the geo location API for personalization

Sitefinity Personalization module retrieves geolocation data based on the IP of the visitor currently browsing the site. This enables you to specify different personalization criteria based on visitor location, for example, city, country, and so on.

Work with Sitefinity CMS IpCityLocationService 

You can leverage the geolocation service functionality in your custom implementation as well. To do this, you first instantiate the Sitefinity IpCityLocationService. To work with the instance of IpCityLocationService you need to check whether it has successfully loaded the GeoLocation database by using its IsReady property.
The IsReady property indicates the service is ready for operation. The property may return false in case you are using the IpCityLocationService too early in your code (for example, in the Global.asax). Thus, you make sure Sitefinity has bootstrapped and read its configurations properly. To use the service in Global.asax, make sure to implement the call to IpCityLocationService in the Bootstrapper_Bootstrapped event handler as demonstrated in the following example:

Get a CityLocation by IP

Once you verify the service is ready, you call its GetLocation method by passing an IPAddress parameter. If a match is found for the specified IP address, IpCityLocationService returns a CityLocation object that contains the City, Country, ISO Code, Region, PostalCode, geographic coordinates of the matched location. Refer to the sample below on how to use IpCityLocationService in your code:


Want to learn more?

Sign up for our free beginner training. Boost your credentials through advanced courses and certification.
Register for Sitefinity training and certification.

Was this article helpful?