locked
ASP.Net MVC: How to add country code to all url by routing or url rewrite RRS feed

  • Question

  • User264732274 posted

    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

    Saturday, July 16, 2016 7:46 PM

Answers

  • User264732274 posted

    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 mfdCookie = new HttpCookie("CountryCookie");
                mfdCookie.Value = data;
                mfdCookie.Expires = DateTime.Now.AddDays(300);
                HttpContext.Current.Response.Cookies.Add(mfdCookie);
            }
    
            public static string ReadCookie()
            {
                var mfdCookieValue = "";
                if (HttpContext.Current.Request.Cookies["CountryCookie"] != null)
                    mfdCookieValue = HttpContext.Current.Request.Cookies["CountryCookie"].Value;
                return mfdCookieValue;
            }
        }

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 18, 2016 11:18 AM