0

I'm working on multi language web site. it works fine. but I need some changes in the URL which it creates.

My Resouces files are in App Global Resources Forlder named like this: lang.resx , lang.ja.resx , lang.fr.resx

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 ML_VoiceRecorderFree1
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{country_code}/{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, country_code="" }
            );
        }
    }
}

and the url whch it creates is this : www.example.com/JP?language=jp and I need this to be like www.example.com/JP

what changes should I do? Appreciate any help. and also here is my controller if needed:

  public async Task<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;
            string userIpAddress = "202.218.119.10";

            var client = new HttpClient
            {
                BaseAddress = new Uri("http://freegeoip.net/xml/")
            };

            var response = await client.GetAsync(userIpAddress);

            var content = await response.Content.ReadAsStringAsync();

            var result = (Response)new XmlSerializer(typeof(Response)).Deserialize(new StringReader(content));
            var country_name = result.CountryName;
            var country_code = result.CountryCode;
            TempData["Country_code"] = country_code;
            TempData["Country_name"] = country_name;

            if (country_code == "FR")
            {
                language = "fr-FR";

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
                return RedirectToAction("Index", new { language = language, country_code = country_code });
            }
            else if (country_code == "JP")
            {
                language = "ja";

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
                return RedirectToAction("Index", new { language = language, country_code = country_code });
            }
            else
            {
                language = "en";

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
                return RedirectToAction("Index", new { language = language, country_code = country_code });

            }


        }
        catch
        {

            language = "en";

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
            return RedirectToAction("Index", new { language = language });
        }
    }

    // do stuff
    return View();


}
neda Derakhshesh
  • 1,103
  • 2
  • 20
  • 43
  • Your route does not include a segment for `language` so its added as a query string parameter. I'm not sure what your wanting to achieve, but I assume you want the parameter to be `country_code`, not `language`, and there is no need to redirect unless the value of the parameter is `null` –  Jun 16 '17 at 11:49
  • really thank you for your care,I want to redirect my URL some how to have its country code included, and I don't want to mention its language code, and if I omit language from this line `return RedirectToAction("Index", new { language = language, country_code = country_code });` then it can not understand threading. so whats your suggestion? thank you again – neda Derakhshesh Jun 16 '17 at 11:56
  • You could make the the route `url: "{country_code}/{controller}/{action}/{language}"`, so its generated as a route value rather than a query string but then you get `www.example.com/JP/jp` which looks a bit odd. I noticed you have asked a few questions regarding this. Give me a chance to have a look at them so I can get a better understanding of what your trying to achieve. –  Jun 16 '17 at 12:13
  • yes exactly I shouldn't have language in my URL. and that's exactly true I'm sticking. I don't know how to appreciate. lots of thanks . @StephenMuecke – neda Derakhshesh Jun 16 '17 at 12:27
  • See [ASP.NET MVC 5 culture in route and url](https://stackoverflow.com/a/32839796). – NightOwl888 Jun 16 '17 at 12:28
  • Rather than using `TempData["Country_code"] = country_code;` which is unnecessary since its in the route already, you can always use `Tempdata` to store the language if you do not want it to be part of the route (but of course, `TempData` will be lost if the user refreshes the browser). Why are you not storing the users language preference so that when they login your can retrieve it? And what about users who may travel and are using a different IP Address - your forcing them to use a different language –  Jun 16 '17 at 12:43
  • I have a drop down in my view which user will change the language and this part is for those who want to change the url manually `if (String.IsNullOrWhiteSpace(language) == false) { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language); Thread.CurrentThread.CurrentUICulture = new CultureInfo(language); }` butits not complete yet.I'm still sticking in IP detection. – neda Derakhshesh Jun 16 '17 at 12:53

0 Answers0