hey*_*uss 4 asp.net asp.net-mvc
开始编写一个简单的过滤器,在每个动作加载时从请求中提取一些东西,从其他堆栈流中复制一些代码,如下所示:
public class TestKeyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext context)
{
if (context.Request.Properties.ContainsKey("test"))
{
// do stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后添加其余的属性:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
GlobalConfiguration.Configuration.Filters.Add(new ElmahHandledErrorLoggerFilter());
filters.Add(new HandleErrorAttribute());
filters.Add(new TestKeyAttribute());
}
Run Code Online (Sandbox Code Playgroud)
在运行时,会导致此错误:
The given filter instance must implement one or more of the following filter
interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter.
Run Code Online (Sandbox Code Playgroud)
我发现的大多数链接都与MVC 3有关,这似乎有效; 然而,我正在使用MVC 4并使用Web API - 我现在需要注册属性吗?
请注意:我不希望附加到Web API控制器的过滤器(将其添加到GlobalConfiguration.Configuration.Filters确实有效),而是普通的Web控制器.
编辑:我知道我可以通过继承IActionFilter而使用OnActionExecuting来实现这一点,我只是好奇为什么这种方法不起作用,因为一堆教程似乎应该说.
我有同样的错误,ElmahHandledErrorLoggerFilter并且实施时感到困惑IExceptionFilter.
经过调查,我踢了自己,我已将其添加filters.Add(new ElmahHandledErrorLoggerFilter());到FilterConfig类下的MVC站点配置中.config.Filters.Add(new ElmahHandleErrorApiAttribute());而是添加到WebApiConfig类工作.
注意:我在这里使用的是WebAPi v1,但我以相同的方式配置了v2项目.
| 归档时间: |
|
| 查看次数: |
4093 次 |
| 最近记录: |