I am using the below routing for my culture requirements:
routes.MapRoute(
name: "SpecificCulture",
url: "{culture}/{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
culture = ""
}
);
And I also have the below filter :
public class CultureFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
var cultureName = (filterContext.RouteData.Values["culture"] as string) ?? string.Empty;
if (!string.IsNullOrWhiteSpace(cultureName) && CultureHelper.IsValidCulture(cultureName))
{
// e.g if requested cultureName is EN-ZZ the below method will get EN-US
cultureName = CultureHelper.GetImplementedCulture(cultureName);
filterContext.RouteData.Values["culture"] = cultureName;
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
}
}
So as you can see on the ActionFilterAttribute
I am getting the approximate culture based on the requested url.
Everything is working fine but I would like to change the url also on the above scenario where the user request's the EN-ZZ and I get the EN-US.
Is it possible ?
Thanks a lot
If you don't need to change the browser url, just try:
var cultureName = (filterContext.RouteData.Values["culture"] as string) ?? string.Empty;
if (string.Compare(cultureName,"EN-ZZ",true) == 0)
{
cultureName = "EN-US";
}
if you need to change the browser url, you need to redirect the user:
var cultureName = (filterContext.RouteData.Values["culture"] as string) ?? string.Empty;
if (string.Compare(cultureName, "EN-ZZ", true) == 0)
{
filterContext.RouteData.Values["culture"] = "EN-US"; //Replace with another culture
filterContext.Result = new RedirectToRouteResult(filterContext.RouteData.Values);
}
else if (!string.IsNullOrWhiteSpace(cultureName) && CultureHelper.IsValidCulture(cultureName))
{
// e.g if requested cultureName is EN-ZZ the below method will get EN-US
cultureName = CultureHelper.GetImplementedCulture(cultureName);
filterContext.RouteData.Values["culture"] = cultureName;
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}