Search code examples
asp.net-mvcweb-servicesipmultilingual

multi language website automatically by their country (ASP.net MVC)


I'm trying to develop a multi language website with asp.net MVC which it should automatically recognize country of clients and then show the web site with their language.
I can get IP Address but I cant find their country. I used a web service here is my controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using global_vrf.GeoIpService;

namespace global_vrf.Controllers
{
public class HomeController : Controller
{
    public ActionResult Index(string language)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
        string userIpAddress = this.Request.UserHostAddress;
        ViewBag.userIpAddress = userIpAddress;



        GeoIPService service = new GeoIPService();
        GeoIP output = service.GetGeoIP(userIpAddress);
        ViewBag.usercountry = output;
        return View();
    }


 }
}

and I have written this in my view to check whats happen

@{
ViewBag.Title = "Home Page";

}
@Resources.Home_txt.AppDes

<br />

@ViewBag.userIpAddress
<br />
@ViewBag.usercountry

and this is the output:

93.110.112.199 
global_vrf.GeoIpService.GeoIP  //this line is showing instead of country name.

Appreciate any help. Thanks


Solution

  • If @ViewBag.userCountry is outputting global_vrf.GeoIpService.GeoIP, then that means that's the type of the thing you're putting into that ViewBag member and that type has no customized ToString overload. When presented with any given type to render, Razor will simply call ToString on it, to get something to actually output. The default for ToString is return the type name and namespace.

    More likely, you'd want to do something like:

    @{ var userCountry = ViewBag.userCountry as global_vrf.GeoIpService.GeoIP; }
    @if (userCountry != null)
    {
        @userCountry.Country;
    }
    

    Where Country would be the property on global_vrf.GeoIpService.GeoIP that actually holds the country name you're looking to actually see output.