Country Tag Helper – part 3

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

 

if (ASPCountryEmpty)
                {
                    string empty = &quot;&lt;option selected style='display: none' value=''&gt;&lt;/option&gt;&quot;;
                    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&lt;string&gt; GetCountryCodeFromIP(string ip)
        {
            //ip= &quot;188.25.145.65&quot;;
            var url = &quot;http://freegeoip.net/csv/&quot;+ip;
            var request = WebRequest.Create(url);
            request.Method = &quot;GET&quot;;
            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];
                    }
                        
                }
                    
            }


        }

Posted

in

,

by

Tags:

Comments

Leave a Reply

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