过滤器分类:
Core WebApi:的几种过滤器:
1.授权过滤器:AuthorizeAttribute.
2.异常过滤器:IExceptionFilter , IAsyncExceptionFilter
3.方法过滤器: ActionfilterAttribute, IActionFilter , IAsyncActionFilter.
后边两种是相对asp.net framwork来讲,asp.net core所特有的
4.资源过滤器:IResourceFilter
5.结果过滤器:ResultFilterAttribute, IResultFilter, IAsyncResultFilter
本文使用第三种IActionFileter来实现日志记录,具体步骤如下:
1.新建一个CustiomActionFilterAttribute的类
public class CustiomActionFilterAttribute : Attribute, IActionFilter//也可以直接继承ActionFilter
{
public void OnActionExecuted(ActionExecutedContext context)
{
Console.WriteLine("this is CustiomActionFilterAttribute.OnActionExecuted");
}
public void OnActionExecuting(ActionExecutingContext context)
{
Console.WriteLine("this is Custio