0

I'm working on multi language web site and everything works fine , but the problem is that when I run the project, I have this URL www.example.com while I want to have it like www.example.com/fr for for example France. I have this problem just for index page rendering for first time, I need to redirect somehow to this www.example.com/fr route, and force it to follow the route config

here is my route config

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.Mvc;
 using System.Web.Routing;
 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 here is my controller

namespace global_vrf.Controllers
{


   public class HomeController : Controller
  {
    public ActionResult Index(string language)
    {

        if (String.IsNullOrWhiteSpace(language) == false)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
        }
        else if (String.IsNullOrWhiteSpace(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";
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
                    Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
                    return View();
                }

                //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";

            }

        }

        return View();     
      }
    }
  }
neda Derakhshesh
  • 1,103
  • 2
  • 20
  • 43

0 Answers0