In this tutorial, you will learn how to find the current users country code using the Episerver geolocation API. When you install Episerver 7, if you look within the webroots App_data folder you should see a file called GeoLiteCity.dat. GeoLiteCity.dat is a geolocation database created by a company called MaxMind that does country code lookup based on IP. This means that within your Episerver website, you can get the current user's IP address and in return get the country code they live in very easily 🔥🔥🔥

If you are running a multi-language site, you can use this feature to automatically redirect a site visitor to the correct language version for their country when they first visit your website . This is a much better user experience, compared to making them manually change the language themselves. If you are new to EPiserver multi-language websites and want to learn how to build one, I recommend reading this tutorial 💥.

To start using geo-location lookups in Episerver you will need to use the Geolocation API. This can be used like this:

As you can see on Line 13, you perform an IP look-up using Lookup(). Lookup()returns a result of typeIGeolocationResult. You can get a lot of useful information fromIGeolocationResult` including:

Get the visitors country code:

Get the visitors region:

Get the visitors continent:

As you can see from these examples, it's very easy to get location data using this database 😊

To get location data from a site visitor, Episerver provides the Geolocation API. Getting location information about a user from this API is pretty straightforward. You create an instance of GeolocationProvider and pass in the current user's IP address. The API will return an IGeolocationResult that allows you to get information such as continent code, country code, region and location. Happy Coding 🤘