我需要根据特定规则设置当前语言。我需要访问当前页面和当前用户才能做出决定。我查看了文档和it said以在PageBase上使用InitializeCulture方法。我的项目是使用MVC而不是WebForms,它与MVC中的InitializeCulture相当吗?
答案 0 :(得分:4)
您可以实施IAuthorizationFilter并在OnAuthorization中进行检查。也可以在IActionFilter中执行此操作,但之前调用OnAuthorization。您将可以访问当前的HttpContext,并从那里获取当前页面数据。
public class LanguageSelectionFilter : IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
// access to HttpContext
var httpContext = filterContext.HttpContext;
// the request's current page
var currentPage = filterContext.RequestContext.GetRoutedData<PageData>();
// TODO: decide which language to use and set them like below
ContentLanguage.Instance.SetCulture("en");
UserInterfaceLanguage.Instance.SetCulture("en");
}
}
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
// register the filter in your FilterConfig file.
filters.Add(new LanguageSelectionFilter());
}
}