MVC 4 Web API注册过滤器 [英] MVC 4 Web API register filter

查看:159
本文介绍了MVC 4 Web API注册过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC 4 Web API为应用程序创建服务层.我正在尝试创建一个全局过滤器,该过滤器将对API的所有传入请求起作用.现在,我了解到,其配置必须与标准MVC全局操作过滤器不同.但是我在网上找到可以找到的所有示例时遇到了问题.

I am using MVC 4 Web API to create a service layer for an application. I am trying to create a global filter that will act on all incoming requests to the API. Now I understand that this has to be configured differently than standard MVC global action filters. But I'm having issues getting any of the examples I'm finding online to work.

我遇到的问题是使用Web API注册过滤器.

The problem I am running into is in registering the filter with Web API.

我这样设置了Global.asax ...

I have my Global.asax set up like this...

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        MVCConfig.RegisterRoutes(RouteTable.Routes);
        MVCConfig.RegisterGlobalFilters(GlobalFilters.Filters);

        WebApiConfig.RegisterRoutes(GlobalConfiguration.Configuration);
        WebApiConfig.RegisterGlobalFilters(GlobalConfiguration.Configuration.Filters);
    }
}

我的标准Mvc路由和过滤器可以正常工作.就像我的WebApi路由一样.这是我的webApi过滤器注册所需要的...

My standard Mvc routing and filters work correctly. As does my WebApi routing. Here is what I have for my webApi filter registration...

public static void RegisterGlobalFilters(System.Web.Http.Filters.HttpFilterCollection filters)
{
    filters.Add(new PerformanceTestFilter());
}

这是PerformanceTestFilter ...

And here is the PerformanceTestFilter...

public class PerformanceTestFilter : ActionFilterAttribute
{
    private readonly Stopwatch _stopWatch = new Stopwatch();

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _stopWatch.Reset();
        _stopWatch.Start();
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        _stopWatch.Stop();
        var executionTime = _stopWatch.ElapsedMilliseconds;
        // Do something with the executionTime
    }
}

此过滤器在标准Mvc GlobalFilterCollection中注册时可以正常工作,但是当我尝试在System.Web.Http.Filters.HttpFilterCollection中注册时,我收到一条错误消息,指出它无法分配给参数类型System.Web. .Http.Filters.IFilter.

This filter works fine when it is registered with the standard Mvc GlobalFilterCollection, but when I try to register it with System.Web.Http.Filters.HttpFilterCollection I get an error saying that it is not assignable to parameter type System.Web.Http.Filters.IFilter.

因此,我假设我的PerformanceTestFilter需要继承自ActionFilterAttribute以外的其他东西,才能注册为webapi过滤器.我只是不确定那是什么.

So I'm assuming that my PerformanceTestFilter needs to inherit from something other than ActionFilterAttribute in order to be registered as a webapi filter. I'm just not sure what that needs to be.

我想我将需要创建两个单独的过滤器以分别与mvc和webapi一起使用.如果有一种创建可以同时注册到两者的过滤器的方法,那就太好了.但是我主要关心的只是使其能够在webapi中使用.

I imagine I will need to create two individual filters to work with mvc and webapi respectively. If there is a way to create a filter that could be registered to both, that would be great. But my primary concern is simply to get it working for webapi.

谢谢

推荐答案

以下内容应该可以工作.实际上,我们将其用于我们的Web API项目.

The following should work. We actually use this for our web API project.

GlobalConfiguration.Configuration.Filters的类型为HttpFilterCollection

 var filters = System.Web.Http.GlobalConfiguration.Configuration.Filters;
 filters.Clear();
 filters.Add(new ValidationActionFilterAttribute());

 public class ValidationActionFilterAttribute : FilterAttribute, IActionFilter, IFilter
 {
    ...
 }

此外,如果您正在同时包含MVC和WebAPI组件的项目中工作,可以检查一下您的命名空间是什么 ActionFilterAttribute的名称空间.这是相当令人困惑的原因 这两个都有两个ActionFilterAttributes:

Also, if you're working in a project that contains both MVC and WebAPI assembilies, could you check what's the namespace your ActionFilterAttribute's namespace. It's fairly confusing cause there are two ActionFilterAttributes under both:

  System.Web.Http.Filters
   System.Web.Http.Mvc

来源:为什么是我的ASP. NET Web API ActionFilterAttribute OnActionExecuting无法触发?

您似乎需要具有两个过滤器,一个用于API,一个用于MVC.您可以将通用代码分解为一个单独的类,然后仅使用特定的过滤器来调用您的通用类,这样就不会违反DRY,本质上是将实际的过滤器用作可注册为过滤器的包装器.

It appears that you will need to have two filters, one for API and one for MVC. You can factor the common code into a separate class, and then just use the specific filter to call through to your common class, thus not violating DRY and essentially using the actual filters as wrappers which can be registered as filters.

这篇关于MVC 4 Web API注册过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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