Country Tag Helper – part 3

Adding empty item – easy peasy. Just append "<option selected style=’display: none’ value=”></option>";

 

if (ASPCountryEmpty)
                {
                    string empty = "<option selected style='display: none' value=''></option>";
                    output.Content.AppendHtml(empty);
                }

For getting the IP, I was trying to balance the 2 variants: downloading GeoIP database (https://dev.maxmind.com/geoip/geoip2/geolite2/) or calling http://freegeoip.net/ .

I do call http://freegeoip.net/ – and the next point is to use Dependency Injection … or a provider to do that.

For the moment, the code is:

 private async Task<string> GetCountryCodeFromIP(string ip)
        {
            //ip= "188.25.145.65";
            var url = "http://freegeoip.net/csv/"+ip;
            var request = WebRequest.Create(url);
            request.Method = "GET";
            using (var wr = await request.GetResponseAsync())
            {
                using (var receiveStream = wr.GetResponseStream())
                {
                    using (var reader = new StreamReader(receiveStream, Encoding.UTF8))
                    {
                        string content = reader.ReadToEnd();
                        return content.Split(',')[1];
                    }
                        
                }
                    
            }


        }