我的 BaseController (MVC 3) 中的重定向有问题 我有两个语言版本的网站波兰语和英语(由$.cookie
插件设置)。如果,设置为英语并且用户想要转到波兰语 URL - 我想要进行正确的重定向。但是,每当我在方法中进行重定向OnActionExecuting
时,在ExecuteCore
方法中,我都会看到语言出于某种原因切换 - 请参阅该(*)
行。
OnActionExecuting
更重要的是,在那种情况下,我得到了和ExecuteCore
方法之间的无限重定向。为什么 ?
public class BaseController : Controller
{
protected override void ExecuteCore()
{
if (Request.Cookies["language"] != null)
{
if (Request.Cookies["language"].Value != "pl-PL" && Request.Cookies["language"].Value != "en-US")
Request.Cookies["language"].Value = "en-US";
}
else
Request.Cookies.Add(new HttpCookie("language", "en-US"));
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(Request.Cookies["language"].Value);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
base.ExecuteCore();
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
switch (filterContext.ActionDescriptor.ActionName)
{
case "Onas":
if (Request.Cookies["language"].Value == "en-US")
{
filterContext.Result = new RedirectResult("/About", true);
return;
}
break;
case "About":
if (Request.Cookies["language"].Value == "pl-PL")
{
(*) filterContext.Result = new RedirectResult("/Onas", true); // Request.Cookies["language"].Value will be en-US inside the ExecuteCore method - why ?
return;
}
break;
}
base.OnActionExecuting(filterContext);
}
}