我已经创建了一个Web Api过滤器(使用System.Web.Http.Filters.ActionFilterAttribute),但我无法让它在ASP.Net MVC 4中运行.我尝试将其添加到RegisterGlobalFilters()方法但是没有用.
因此,如果使用ASP.Net MVC中托管的Web Api,如何注册过滤器?
我正在尝试创建自己的过滤器属性,以支持多语言.这个想法很简单.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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里定义它.
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 = …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc multilingual inheritance interface-implementation