0

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

1 Answer 1

1

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

            }

        }
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your care. it has an error Error 1 A namespace cannot directly contain members such as fields or methods .how does it work then?need some explain if possible
RoutePrefix[("admin/index")]. And above action route[("index")] and it's works only in MVC 5
I think that's not useful for me that much because my language is going to be set on my index action not before
i think my codes are complicated. I will work more on it. any way thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.