Tridip Bhattacharjee

Tridip Bhattacharjee

  • NA
  • 1.2k
  • 78.2k

ASP.Net WebForm: rewrite rule is not working in my web.confi

Nov 16 2017 2:26 PM
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. <!--    
  3.   For more information on how to configure your ASP.NET application, please visit    
  4.   http://go.microsoft.com/fwlink/?LinkId=169433    
  5.   -->    
  6. <configuration>    
  7.   <system.web>    
  8.     <compilation debug="true" targetFramework="4.5" />    
  9.     <httpRuntime targetFramework="4.5" />    
  10.     <pages>    
  11.       <namespaces>    
  12.         <add namespace="System.Web.Optimization" />    
  13.       </namespaces>    
  14.       <controls>    
  15.         <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />    
  16.       </controls>    
  17.     </pages>    
  18.   </system.web>    
  19.   <runtime>    
  20.     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">    
  21.       <dependentAssembly>    
  22.         <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />    
  23.         <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />    
  24.       </dependentAssembly>    
  25.       <dependentAssembly>    
  26.         <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />    
  27.         <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />    
  28.       </dependentAssembly>          
  29.     </assemblyBinding>    
  30.   </runtime>    
  31.   <system.webServer>    
  32.   <rewrite>    
  33.     <rules>    
  34.       <rule name="Rewrite language code">    
  35.         <match url="^([a-z]+)/([0-9a-z]+).aspx" />    
  36.         <action type="Rewrite" url="/{R:2}.aspx?lang={R:1}" />    
  37.       </rule>          
  38.     </rules>    
  39.   </rewrite>    
  40.   </system.webServer>    
  41. </configuration>    
 probably something i have missed for my rewrite rule for which it is not working. so any one can tell me what is mistake in my code or in my rewrite rule ?
 
i am running site from VS2013 IDE and that is why i have not installed IIS rewrite module for IIS.