2017-06-03 121 views
0

我正在使用多語言網站。它對每個IP_Address都可以正常工作,問題是我想在URL呈現後更改URL,方法是在URL中顯示什麼是語言代碼。 這裏是我的路由配置如何更改多語言網站上的網址 - Asp.Net MVC

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

使用屬性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"; 

      } 

     } 
+0

感謝您的關心路由。它有一個錯誤'錯誤1一個命名空間不能直接包含成員,如字段或方法'.how does it work then?need some explain if if possible –

+0

RoutePrefix [(「admin/index」)]。和上面的行動路線[(「索引」)],它只適用於MVC 5 – NIts577

+0

我認爲這對我來說沒有多大用處,因爲我的語言將設置在我的索引操作上,而不是在 –