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.