我使用Afana solution构建了一个多语言网站。
我做了一些更改,为了某些SEO目的,将文化添加到URL。
现在我有一个像下面这样的BaseController以及从这个控制器中删除的每个控制器。
public class BaseController : Controller
{
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
string cultureName = RouteData.Values["culture"] as string;
if (string.IsNullOrEmpty(cultureName))
{
HttpCookie cultureCookie = Request.Cookies["_culture_"];
if (cultureCookie != null)
cultureName = cultureCookie.Value;
else
cultureName = CultureHelper.GetDefaultCulture();
}
cultureName = CultureHelper.GetImplementedCulture(cultureName);
if (RouteData.Values["culture"] as string != cultureName)
{
RouteData.Values["culture"] = cultureName.ToLowerInvariant();
Response.RedirectToRoute(RouteData.Values);
}
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
return base.BeginExecuteCore(callback, state);
}
}
这是路由配置:
routes.MapRoute(
name: "WithCulture",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = String.Empty, controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { culture = "[a-z]{2}" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { culture = String.Empty, controller = "Home", action = "Index", id = UrlParameter.Optional }
);
它(似乎)运作良好。我的意思是我从这个链接重定向:
/
/Home
到此:
/fr
/fr/Home
但是当我添加一个区域(例如经理)。 在这一点上,我得到一些URL的错误。例如:
/
/Home/
重定向到此网址
/Manager/Home?culture=en
我错过了什么?