Search code examples
asp.net-mvcurlmultilingual

how to change URL in multi language web site- Asp.Net MVC


I am working on multi language web site. it works fine with each IP_Address, the problem is that I want to make the URL changed after it renders, in the way it shows whats the language code in the URL. here is my route config

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

and this 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)
    {
      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";

            }

        }

Appreciate any help. thanks


Solution

  • Use attribute routing in 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";
    
                }
    
            }