1

i have two requirement

1st one is : my requirement is to add country code to all url. before my url look like

www.mysite.net/catalog.aspx

www.mysite.net/product.aspx

www.mysite.net/cart.aspx

but now i have to write some code in global place which will inject 2 char country code in all url just reading cookie value. so my new url would look like

www.mysite.net/uk/catalog.aspx

www.mysite.net/uk/product.aspx

www.mysite.net/uk/cart.aspx

so tell me what code i need to add in global area of web site which inject country code in url.

2nd one is : whe country code will be injected in the url in browser address bar then my all links in my all pages should have the same country code.

if possible discuss this with code example and hints. one guy told me to download and install the IIS URL Rewrite module but i do not know how IIS IIS URL Rewrite module can inject country code reading from country cookie. i guess i need to add some code for that.

can i solve it by url routing which is in asp.net mvc. looking for help.

thanks

Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94
  • 2
    If you have the culture in the URL, no cookie is necessary. You don't *inject* the culture into the URL, the user *requests* the culture through the URL. See: [ASP.NET MVC 5 culture in route and url](http://stackoverflow.com/a/32839796/181087). – NightOwl888 Jul 16 '16 at 19:53
  • my site has many country flag. so when user click on any country flag then we drop cookie with country code in user pc and show localize data based on country code but when user come first time then no cookie exist and the default country becomes GB. – Monojit Sarkar Jul 16 '16 at 20:03
  • 2
    Still doesn't mean you need a cookie. If the information you need to localize is already in the URL, you don't need it (and shouldn't use it). Localization is *content*, not *personalization*. You should put each culture in the URL so it can be indexed by search engines in native languages, not hide it behind a cookie that search engines will never provide. – NightOwl888 Jul 16 '16 at 20:08
  • my web site is based on cookie. basically i have a parent web site which is developed with webform and now i have to develop another small web site with mvc which will run under the my main web site and read cookie from my main web site. if cookie not found then GB would be default country. what i need to do that when i will start my mvc site from IDE then default url look like localhost:2010/uk or localhost:2010/de based on cookie value ? give me some idea. – Monojit Sarkar Jul 16 '16 at 20:25
  • how could i change all links in my page as a result all link should have country code in it automatically. for this do i need to use url rewrite module ? if yes then how url rewrite module rule should look like to change all url and add country code in it? – Monojit Sarkar Jul 16 '16 at 20:27
  • @NightOwl888 any article with sample code exist for implement country code in url with asp.net mvc.......if so please share the link from where i can download the source code , solution and run in my pc. thanks – Monojit Sarkar Jul 16 '16 at 20:33
  • .NET Routing works in both environments. [See this](http://stackoverflow.com/a/32839796/181087) for MVC. [See this](http://forums.asp.net/t/1669582.aspx?Include+Culture+Info+in+the+URL) for ASP.NET. This is not what URL Rewrite is really for, since your application will need to read this value. You might be able to use a custom handler (as in the last link) in both environments...the only thing you really need to do is transfer the setting from the URL to the current thread (whereever it is requested from) and then use resources to load the localized data. – NightOwl888 Jul 16 '16 at 20:38
  • i have to give some feature to user as a result user should be able to switch from one language to another language. suppose from english to Germany. how it will be possible ? also how to persist the user language change ? do i need to store user selected language in cookie or session ? – Monojit Sarkar Jul 16 '16 at 20:43
  • *Read* the first link I sent you. In MVC, the route values are automatically reused from the request to generate the URLs, so they will all generate with the *current* culture in them. I am not sure if it works the same way in ASP.NET, but if not it is just a matter of adding the culture manually when generating the URL. – NightOwl888 Jul 16 '16 at 20:45
  • 1
    The user just *requests* a URL, and the whole site switches to that culture. All you need to do is make a simple control to switch between URLs and the job is done (both simpler than and better than with a cookie). Once a culture is requested, it will "stick" until another one is requested as long as you generate the URLs using routing. – NightOwl888 Jul 16 '16 at 20:47

1 Answers1

0

This way i have done the job. so i like to share that may help others.

my Application_BeginRequest looks like

 protected void Application_BeginRequest(Object sender, EventArgs e)
        {
            string CountryCodeInUrl = "", redirectUrl="";
            var countryCode = CookieSettings.ReadCookie();
            if (countryCode=="")
            {
                countryCode = "gb";
            }

            if (HttpContext.Current.Request.RawUrl.Length >= 2)
            {
                CountryCodeInUrl = HttpContext.Current.Request.RawUrl.Substring(1, 2);
            }

            if (countryCode != CountryCodeInUrl)
            {
                if (HttpContext.Current.Request.RawUrl.Length >= 2)
                {
                    if (HttpContext.Current.Request.RawUrl.Substring(1, 2) != "")
                    {
                        countryCode = HttpContext.Current.Request.RawUrl.Substring(1, 2);
                    }
                }

                if(!System.Web.HttpContext.Current.Request.RawUrl.Contains(countryCode))
                {
                    redirectUrl = string.Format("/{0}{1}", countryCode, System.Web.HttpContext.Current.Request.RawUrl);
                }
                else
                {
                    redirectUrl = System.Web.HttpContext.Current.Request.RawUrl;
                }
                CookieSettings.SaveCookie(countryCode);
                System.Web.HttpContext.Current.Response.RedirectPermanent(redirectUrl);
            }

        }

CookieSettings class

public class CookieSettings
    {
        public static void SaveCookie(string data)
        {
            var _CookieValue= new HttpCookie("CountryCookie");
            _CookieValue.Value = data;
            _CookieValue.Expires = DateTime.Now.AddDays(300);
            HttpContext.Current.Response.Cookies.Add(_CookieValue);
        }

        public static string ReadCookie()
        {
            var _CookieValue = "";
            if (HttpContext.Current.Request.Cookies["CountryCookie"] != null)
                _CookieValue= HttpContext.Current.Request.Cookies["CountryCookie"].Value;
            return _CookieValue;
        }
    }

routine config look like

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

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

Home controller index action look like

public ActionResult Index(string countrycode)
{
    return View();
}

this issue is over.

Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94