多语言网站自动由他们的国家(ASP.net MVC)
multi language website automatically by their country (ASP.net MVC)
我正在尝试使用 asp.net MVC 开发一个多语言网站,它应该自动识别客户所在的国家/地区,然后用他们的语言显示网站。
我可以获得 IP 地址,但找不到他们的国家/地区。我使用了网络服务
这是我的控制器
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();
}
}
}
我写这篇文章是为了检查发生了什么
@{
ViewBag.Title = "Home Page";
}
@Resources.Home_txt.AppDes
<br />
@ViewBag.userIpAddress
<br />
@ViewBag.usercountry
这是输出:
93.110.112.199
global_vrf.GeoIpService.GeoIP //this line is showing instead of country name.
感谢任何帮助。
谢谢
请使用 geoipresult,然后获取国家/地区名称
<GetGeoIPResult>
<CountryName>string</CountryName>
<CountryCode>string</CountryCode>
</GetGeoIPResult>
如果 @ViewBag.userCountry
正在输出 global_vrf.GeoIpService.GeoIP
,那么这意味着这是您要放入 ViewBag
成员的东西的 类型 并且该类型没有自定义 ToString
重载。当呈现任何给定类型进行渲染时,Razor 将简单地对其调用 ToString
,以获得实际输出的内容。 ToString
的默认值是 return 类型名称和命名空间。
您更有可能想要执行以下操作:
@{ var userCountry = ViewBag.userCountry as global_vrf.GeoIpService.GeoIP; }
@if (userCountry != null)
{
@userCountry.Country;
}
其中 Country
是 global_vrf.GeoIpService.GeoIP
上的 属性,它实际上包含您要实际查看输出的国家/地区名称。
我正在尝试使用 asp.net MVC 开发一个多语言网站,它应该自动识别客户所在的国家/地区,然后用他们的语言显示网站。
我可以获得 IP 地址,但找不到他们的国家/地区。我使用了网络服务
这是我的控制器
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();
}
}
}
我写这篇文章是为了检查发生了什么
@{
ViewBag.Title = "Home Page";
}
@Resources.Home_txt.AppDes
<br />
@ViewBag.userIpAddress
<br />
@ViewBag.usercountry
这是输出:
93.110.112.199
global_vrf.GeoIpService.GeoIP //this line is showing instead of country name.
感谢任何帮助。 谢谢
请使用 geoipresult,然后获取国家/地区名称
<GetGeoIPResult>
<CountryName>string</CountryName>
<CountryCode>string</CountryCode>
</GetGeoIPResult>
如果 @ViewBag.userCountry
正在输出 global_vrf.GeoIpService.GeoIP
,那么这意味着这是您要放入 ViewBag
成员的东西的 类型 并且该类型没有自定义 ToString
重载。当呈现任何给定类型进行渲染时,Razor 将简单地对其调用 ToString
,以获得实际输出的内容。 ToString
的默认值是 return 类型名称和命名空间。
您更有可能想要执行以下操作:
@{ var userCountry = ViewBag.userCountry as global_vrf.GeoIpService.GeoIP; }
@if (userCountry != null)
{
@userCountry.Country;
}
其中 Country
是 global_vrf.GeoIpService.GeoIP
上的 属性,它实际上包含您要实际查看输出的国家/地区名称。