2017-06-03 2 views
0

Ich arbeite auf mehrsprachige Website. es funktioniert gut mit jeder IP_Address, das Problem ist, dass ich die URL geändert werden möchte, nachdem es rendert, in der Art, wie es zeigt, was der Sprachencode in der URL ist. hier ist meine Route Configwie URL in mehrsprachigen Website-Asp.Net MVC ändern

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="" } 
     ); 
    } 
} 
} 

und dies ist mein 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) 
    { 
     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"; 

      } 

     } 

jede Hilfe dankbar. Dank

Antwort

1

Attribut Verwendung in Routing-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

Dank für Ihre Pflege. Es hat einen Fehler 'Fehler 1 Ein Namespace kann nicht direkt Mitglieder wie Felder oder Methoden enthalten.'wie funktioniert es dann?', wenn möglich, erkläre wenn möglich –

+0

RoutePrefix [("admin/index")]. Und über Aktionsroute [("Index")] und es funktioniert nur in MVC 5 – NIts577

+0

Ich denke, das ist nicht so nützlich für mich, weil meine Sprache auf meine Indexaktion nicht eingestellt wird –