我想,以支持多语言来创建自己的过滤属性。 这个想法很简单。 URL代表语言。
- * HTTP://host.ext/ EN / rest_of_the_url *将用英语开
- * HTTP://host.ext/ HY / rest_of_the_url *将在亚美尼亚打开。
问题是,在运行它说,MultilingualActionFilterAttribute
以下是错误的文字“给定的过滤器实例必须实现以下过滤器接口中的一个或多个:个IAuthorizationFilter,IActionFilter,IResultFilter,个IExceptionFilter”
在这里,我用它作为全局筛选器。
namespace TIKSN.STOZE.WebApp
{
public class FilterConfig
{
public static void RegisterGlobalFilters(System.Web.Mvc.GlobalFilterCollection filters)
{
filters.Add(new TIKSN.STOZE.Common.MultilingualActionFilterAttribute());
filters.Add(new System.Web.Mvc.HandleErrorAttribute());
}
}
}
在这里,我定义它。
namespace TIKSN.STOZE.Common
{
public class MultilingualActionFilterAttribute : System.Web.Mvc.ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
{
string language = System.Convert.ToString(filterContext.RouteData.Values["language"]);
System.Diagnostics.Debug.Print("Requested language is '{0}'", language);
language = Helper.PickUpSupportedLanguage(language);
System.Diagnostics.Debug.Print("Supported language is '{0}'", language);
if (language == string.Empty)
{
filterContext.HttpContext.Response.RedirectToRoutePermanent(new { language = Common.Properties.Settings.Default.DefaultLanguageCode });
}
language = Helper.TryToPickUpSupportedLanguage(language);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(language);
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(language);
}
}
}