如何更改多语言网站中的URL- Asp.Net MVC

时间:2017-06-03 10:32:07

标签: asp.net-mvc url multilingual

我正在开发多语言网站。它适用于每个IP_Address,问题是我想在渲染后更改URL,就像它显示URL中的语言代码一样。 这是我的路线配置

namespace global_vrf
{
  public class RouteConfig
   {
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{language}/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, language="" }
        );
    }
 }
} 

这是我的控制者:

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)
    {
      if (language!="") 
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
        }
        else if(language=="") 
        {
            try
            {
                string userIpAddress = this.Request.UserHostAddress;
                ViewBag.userIpAddress = userIpAddress;

                GeoIPService service = new GeoIPService();
                GeoIP output = service.GetGeoIP(userIpAddress);
                ViewBag.userIpAddress = userIpAddress;
                var country_name = output.CountryName;
                ViewBag.cnam = country_name;
                var country_code = output.CountryCode;
                ViewBag.ccode = country_code;

                 if (country_code == "FRA")
                {
                    language = "fr-FR";
                }
                    //and I will check the other languages here


            }
            catch
            {
                string userIpAddress = "209.95.51.176";
                ViewBag.userIpAddress = userIpAddress;

                GeoIPService service = new GeoIPService();
                GeoIP output = service.GetGeoIP(userIpAddress);
                ViewBag.userIpAddress = userIpAddress;
                var country_name = output.CountryName;
                ViewBag.cnam = country_name;
                var country_code = output.CountryCode;
                ViewBag.ccode = country_code;
                language = "en-us";

            }

        }

感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:1)

在mvc中使用属性路由

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
{
RoutePrefix("Example Name")]
  public class HomeController : Controller
  {
    public ActionResult Index(string language)
    {
      if (language!="") 
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
        }
        else if(language=="") 
        {
            try
            {
                string userIpAddress = this.Request.UserHostAddress;
                ViewBag.userIpAddress = userIpAddress;

                GeoIPService service = new GeoIPService();
                GeoIP output = service.GetGeoIP(userIpAddress);
                ViewBag.userIpAddress = userIpAddress;
                var country_name = output.CountryName;
                ViewBag.cnam = country_name;
                var country_code = output.CountryCode;
                ViewBag.ccode = country_code;

                 if (country_code == "FRA")
                {
                    language = "fr-FR";
                }
                    //and I will check the other languages here


            }
            catch
            {
                string userIpAddress = "209.95.51.176";
                ViewBag.userIpAddress = userIpAddress;

                GeoIPService service = new GeoIPService();
                GeoIP output = service.GetGeoIP(userIpAddress);
                ViewBag.userIpAddress = userIpAddress;
                var country_name = output.CountryName;
                ViewBag.cnam = country_name;
                var country_code = output.CountryCode;
                ViewBag.ccode = country_code;
                language = "en-us";

            }

        }