在MVC4表演和错误,我要实现一些接口,但我已经做到了 [英] in MVC4 shows and error that I have to implement some Interface but I am already done it

查看:612
本文介绍了在MVC4表演和错误,我要实现一些接口,但我已经做到了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,以支持多语言来创建自己的筛选器属性。
这个想法很简单。 URL代表语言。


  • * HTTP://host.ext/的连接 / rest_of_the_url *将用英语开

  • * HTTP://host.ext/ HY / rest_of_the_url *将在亚美尼亚打开

的问题是,在运行它说MultilingualActionFilterAttribute

下面是错误文本给定的过滤器实例必须实现以下过滤器接口中的一个或多个:个IAuthorizationFilter,IActionFilter,IResultFilter,个IExceptionFilter

在这里,我用它作为全局过滤器。

 命名空间TIKSN.STOZE.WebApp
{
    公共类一个FilterConfig
    {
        公共静态无效RegisterGlobalFilters(System.Web.Mvc.GlobalFilterCollection过滤器)
        {
            filters.Add(新TIKSN.STOZE.Common.MultilingualActionFilterAttribute());
            filters.Add(新System.Web.Mvc.HandleErrorAttribute());
        }
    }
}

在这里,我定义它。

 命名空间TIKSN.STOZE.Common
{
    公共类MultilingualActionFilterAttribute:System.Web.Mvc.ActionFilterAttribute
    {
        公共覆盖无效OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            String语言= System.Convert.ToString(filterContext.RouteData.Values​​ [语言]);            System.Diagnostics.Debug.Print(请求的语言为{0}语);
            语言= Helper.PickUpSupportedLanguage(语言);
            System.Diagnostics.Debug.Print(支持的语言为{0}语);            如果(语言==的String.Empty)
            {
                filterContext.HttpContext.Response.RedirectToRoutePermanent(新{语言= Common.Properties.Settings.Default.DefaultLanguage code});
            }            语言= Helper.TryToPickUpSupportedLanguage(语言);            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(语言);
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(语言);
        }
    }
}


解决方案

的问题是,我更新到MVC 5,所以我不得不更新的的web.config 的文件了。
看<一个href=\"http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2\"相对=nofollow>这里。

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

解决方案

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

这篇关于在MVC4表演和错误,我要实现一些接口,但我已经做到了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆