i am using webform and my .net framework version is 4.5. i need to inject country code in url. i could inject it from global.asax file.
 
here is the code
  1. protected void Application_BeginRequest(Object sender, EventArgs e)    
  2. {    
  3.     string rawUrl = HttpContext.Current.Request.RawUrl;    
  4.     
  5.     //check for 2 characters wrapped in forward slashes eg. /en/    
  6.     string countryCode = Utility.GetCountryCodeFromUrl(rawUrl);    
  7.     SiteCulture Culture = SiteGlobalization.DefaultCulture;    
  8.     
  9.     
  10.     if (string.IsNullOrEmpty(countryCode))    
  11.     {    
  12.         //check cookie    
  13.         if (!string.IsNullOrEmpty(Utility.GetCookieValue()))    
  14.         {    
  15.             string threeDigitIsoRegionCodeCookie = Utility.GetCookieValue();    
  16.             Culture = SiteGlobalization.BBACultures.Values.FirstOrDefault(x => x.ThreeDigitISORegionCode.Equals(threeDigitIsoRegionCodeCookie, StringComparison.InvariantCultureIgnoreCase));    
  17.             if (Culture != null && !string.IsNullOrEmpty(Culture.TwoDigitISORegionCode))    
  18.             {    
  19.                 countryCode = Culture.TwoDigitISORegionCode;    
  20.             }    
  21.         }    
  22.     
  23.         //check the referer.    
  24.         if (string.IsNullOrEmpty(countryCode) && HttpContext.Current.Request.UrlReferrer != null && !string.IsNullOrEmpty(HttpContext.Current.Request.UrlReferrer.AbsolutePath))    
  25.         {    
  26.             countryCode = Utility.GetCountryCodeFromUrl(HttpContext.Current.Request.UrlReferrer.AbsolutePath);    
  27.         }    
  28.     
  29.         //finally browser    
  30.         countryCode = string.IsNullOrEmpty(countryCode) ? Utility.Get2DigitCountryCodeFromBrowser().ToLower() : countryCode;    
  31.     
  32.         string redirectUrl = string.Format("/{0}{1}", countryCode, rawUrl);    
  33.         HttpContext.Current.Response.RedirectPermanent(redirectUrl.ToLower());    
  34.     }    
  35.     
  36.     /* is the country code valid ? */    
  37.     Culture = SiteGlobalization.BBACultures.Values.FirstOrDefault(x => x.TwoDigitISORegionCode.Equals(countryCode, StringComparison.InvariantCultureIgnoreCase));    
  38.     
  39.     if (Culture == null || string.IsNullOrEmpty(Culture.ThreeDigitISORegionCode))    
  40.     {    
  41.         HttpContext.Current.Response.RedirectPermanent(string.Format("/{0}/index.aspx", SiteGlobalization.DefaultCulture.TwoDigitISORegionCode.ToLower()));    
  42.     }    
  43.     
  44.     if (HttpContext.Current.Request.FilePath.EndsWith("/" + countryCode))    
  45.     {    
  46.         HttpContext.Current.Response.RedirectPermanent(string.Format("/{0}/index.aspx", countryCode.ToLower()));    
  47.     }    
  48.     
  49.     /* set the default cutlure */    
  50.     Utility.SetCountry(Culture);    
  51. }    
the above code is working and injecting country code as per cookie value. so when i run my project from VS2013 IDE then url look like http://localhost:53741/gb/ or http://localhost:53741/gb/default
but getting error when browsing from IDE. because there is no folder called gb in my project.
so i add rewrite rule and that is as follows
  1. <rewrite>  
  2. <rules>  
  3. <rule name="Rewrite language code">  
  4. <match url="^([a-z]+)/([0-9a-z]+).aspx" />  
  5. <action type="Rewrite" url="/{R:2}.aspx?lang={R:1}" />  
  6. rule>  
  7. rules>  
  8. rewrite>  
my full web.config code as follows with rewrite rule
  1. xml version="1.0" encoding="utf-8"?>    
  2. answer this question