[英]ASP.NET MVC - Routing and ActionFilterAttribute
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. 因此,正如您在
ActionFilterAttribute
上看到的那样,我正在根据请求的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. 一切工作正常,但我想在上述情况下也更改URL,在这种情况下,用户要求输入EN-ZZ,而我得到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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.