11

I am trying to create own filter attribute in order to support multilinguality. The idea is simple. URL stands for language.

  • *http://host.ext/en/rest_of_the_url* will open in English and
  • *http://host.ext/hy/rest_of_the_url* will open in Armenian.

The problem is that at run it says that MultilingualActionFilterAttribute

Here is the error text "The given filter instance must implement one or more of the following filter interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter."

Here I am using it as global filter.

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());
        }
    }
}

Here I am defining it.

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);
        }
    }
}
TIKSN
  • 615
  • 1
  • 6
  • 24
  • That all looks OK. Is this a brand new MVC4 site or one upgraded from MVC3? It could be a dll version issue - have you tried deleting your temporary ASP.NET files? – levelnis Dec 04 '12 at 19:22

3 Answers3

26

if you are using web api then the problem may happen because of implementing wrong interface, as IActionFilter is defined in both System.Web.Http.Filters and System.Web.Mvc namespaces.

Behnam Esmaili
  • 5,835
  • 6
  • 32
  • 63
  • 1
    No, there are not any 'using'-s. I have implemented System.Web.Mvc.ActionFilterAttribute class, which itself implements System.Web.Mvc.IActionFilter interface. – TIKSN Dec 04 '12 at 20:20
  • 7
    Web API filters are registered using GlobalConfiguration.Configuration.Filters. Check [this](http://forums.asp.net/t/1835666.aspx) for more info. – Florin Dumitrescu May 21 '13 at 19:16
3

The problem was that I updated to MVC 5, so I had to update web.config files too. Look here.

TIKSN
  • 615
  • 1
  • 6
  • 24
2

This one works:

Please add your filter to the webApiconfig instead of filter config file.

From A. Murray:

https://stackoverflow.com/a/32518692/4853768

Community
  • 1
  • 1