在MVC4中显示和错误,我必须实现一些接口,但我已经完成了它

时间:2012-12-04 19:07:26

标签: c# asp.net-mvc inheritance multilingual interface-implementation

我正在尝试创建自己的过滤器属性以支持多语言。 这个想法很简单。 URL代表语言。

  • * http://host.ext/ zh_ / 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);
        }
    }
}

3 个答案:

答案 0 :(得分:25)

如果您使用的是web api,则可能会因为实现错误的界面而导致问题,因为IActionFilterSystem.Web.Http.Filters命名空间中都定义了System.Web.Mvc

答案 1 :(得分:3)

问题是我更新到MVC 5,所以我不得不更新 web.config 文件。 看here

答案 2 :(得分:2)

这个有效:

请将您的过滤器添加到webApiconfig而不是过滤器配置文件。

来自A. Murray:

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