Web API过滤器

时间:2023-03-10 05:06:28
Web API过滤器

Web API包含在操作方法执行之前或之后添加额外的逻辑的过滤器。过滤器可用于提供横切特性,比如日志记录、异常处理、性能测量、身份验证和授权等等。

过滤器可以应用于Web API控制器或一个或多个操作方法上的属性。每个过滤器是必须实现System.Web.Http.Filters命名空间中的IFilter接口的类。然而,System.Web.Http.Filters命名空间还包括其他其他可用于创建特定过滤器的接口和类。

下表列出了可用于创建Web API过滤器的重要的接口和类。

过滤器类型 接口 描述
简单的过滤 IFilter - 定义一个过滤器中使用的方法
Action方法过滤器 IActionFilter ActionFilterAttribute 用于添加额外的逻辑操作方法执行之前或之后。
身份验证过滤器 IAuthenticationFilter - 用于迫使用户或客户执行操作方法之前验证。
授权过滤器 IAuthorizationFilter AuthorizationFilterAttribute 用来限制特定的用户或组访问操作方法。
异常过滤器 IExceptionFilter ExceptionFilterAttribute 用于处理Web API所有未处理的异常。
覆盖过滤器 IOverrideFilter - 用于定制其他过滤器的行为。

正如你所看到的,上面的表列举了一些过滤器器类型的类以及接口。接口包含了您的自定义属性类中必须实现的方法,而过滤器类则已经实现了这些方法。这些方法便于Web API可以覆盖添加额外的逻辑。例如,ActionFilterAttribute类包括可以覆盖的方法。我们只需要覆盖想要覆盖的方法,但是如果你使用IActionFilter属性,你必须实现的所有方法。

访问MSDN了解System.Web.Http.Filters命名空间下的所有可用的类和接口。

让我们创建一个用来记录日志的LogAttribute类,来展示Action过滤器。

首先,创建一个继承自ActionFilterAttribute类的LogAttribute类,如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class LogAttribute : ActionFilterAttribute 
 {
    public LogAttribute()
    {
    }
        
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        Trace.WriteLine(string.Format("Action Method {0} executing at {1}", actionContext.ActionDescriptor.ActionName, DateTime.Now.ToShortDateString()), "Web API Logs");
    }
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        Trace.WriteLine(string.Format("Action Method {0} executed at {1}", actionExecutedContext.ActionContext.ActionDescriptor.ActionName, DateTime.Now.ToShortDateString()), "Web API Logs");
    }
}

在上面的例子中,LogAttribute来源于ActionFilterAttribute类并覆盖OnActionExecuting和OnActionExecuted方法用来记录日志到跟踪侦听器。(你也可以使用你自己的日志类来记录日志到文本文件或其他介质。)

创建LogAttribute类的另一种方法是通过实现IActionFilter接口并派生属性类,如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class LogAttribute : Attribute, IActionFilter
{
    public LogAttribute()
    {
    }
    public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
    {
        Trace.WriteLine(string.Format("Action Method {0} executing at {1}", actionContext.ActionDescriptor.ActionName, DateTime.Now.ToShortDateString()), "Web API Logs");
        var result = continuation();
        result.Wait();
             
        Trace.WriteLine(string.Format("Action Method {0} executed at {1}", actionContext.ActionDescriptor.ActionName, DateTime.Now.ToShortDateString()), "Web API Logs");
        return result;
    }
    public bool AllowMultiple
    {
        get return true; }
    }
}

在上面的例子中,继承于属性类使其是一个属性,实现IActionFilter使LogAttribute类是一个Action过滤器。

现在,您可以在控制器或操作方法中应用[Log]属性如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
[Log]
public class StudentController : ApiController
{
    public StudentController()
    {
             
    }
    public Student Get()
    {
        //provide implementation  
    }
}

现在,它将记录所有StudentController的请求。因此您可以用这种方法对横切关注点创建过滤器。

出处:http://www.yuanjiaocheng.net/webapi/webapi-filters.html#Web API过滤器