[TOC] ___ #作用 - 在請求AuthorizeFilter -> ResourceFilter -> ActionFilter, 可以記錄Action的日誌 - ActionFilter 在控制器實例化之後執行 - ResourceFilter 可以在全局, Controller, Act ...
目錄
作用
- 在請求AuthorizeFilter -> ResourceFilter -> ActionFilter, 可以記錄Action的日誌
- ActionFilter 在控制器實例化之後執行
- ResourceFilter 可以在全局, Controller, Action 都可以設置, 並且都會執行(一個ResourceFilter可以重覆設置)
如果都設置執行順序為:
- 全局
- Controller
- Action
- Action 方法
- Action
- Controller
- 全局
實現
IActionFilter
- 需要繼承 Attribute 並 並實現 IActionFilter
- 實現介面方法
執行順序為:
- OnActionExecuting
- Action
- OnActionExecuted
IAsyncActionFilter
- 需要繼承 Attribute 並 並實現 IAsyncActionFilter
- 實現介面方法
- 該介面只提供一個 OnActionExecutionAsync方法,如果想執行ActionExecutedContext方法,需要執行方法中ActionExecutionDelegate委托並取返回值然後代碼在執行為
ActionExecutedContext方法
執行順序為:
- OnActionExecuting
- Action
- OnActionExecuted
ActionFilterAttribute
- 需要繼承 ActionFilterAttribute
- 重寫 OnActionExecuting OnActionExecuted OnResultExecuting OnResultExecuted 方法
執行順序為:
- OnActionExecuting
- Action
- OnActionExecuted
- OnResultExecuting
- OnResultExecuted
Aop Action執行
過濾器代碼
using Microsoft.AspNetCore.Mvc.Filters;
namespace Cnpc.Com.Ioc.WebApp.Filter.ActionFilter
{
public class CustomAsyncActionFilter : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
{
Console.WriteLine("ActionExecutingContext.....");
}
ActionExecutedContext executed = await next();
{
Console.WriteLine("ActionExecutedContext.....");
}
}
}
}
Action代碼
using Cnpc.Com.Ioc.WebApp.Filter;
using Microsoft.AspNetCore.Mvc;
namespace Cnpc.Com.Ioc.WebApp.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class TestFilterController : ControllerBase
{
//[TypeFilter(typeof(CustomAsyncActionFilter))] 如果想在ActionFilter支持Nlog 並使用構造註入就這樣寫
[CustomAsyncActionFilter]
[HttpGet]
public async Task Action_AsyncActionFilter()
{
Console.WriteLine("Func...");
await Task.CompletedTask;
}
}
}
使用日誌
Action
//[ServiceFilter(typeof(CustomAsyncActionFilter))] 如果使用SerciceFilter 要先將CustomAsyncActionFilter 註冊到ioc中
[TypeFilter(typeof(CustomAsyncActionFilter))] //如果想在ActionFilter支持Nlog 並使用構造註入就這樣寫
[HttpGet]
public async Task Action_AsyncActionFilter()
{
Console.WriteLine("Func...");
await Task.CompletedTask;
}
CustomAsyncActionFilter.cs
using Cnpc.Com.Ioc.IBll;
using Cnpc.Com.Ioc.IDal;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Cnpc.Com.Ioc.WebApp.Filter.ActionFilter
{
public class CustomAsyncActionFilter : Attribute, IAsyncActionFilter
{
ILogger<CustomAsyncActionFilter> logger { get; set; }
IStudent student { get; set; }
IWrite write { get;set; }
public CustomAsyncActionFilter(ILogger<CustomAsyncActionFilter> logger,IStudent student,IWrite write)
{
this.logger = logger;
this.student = student;
this.write = write;
}
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
{
Console.WriteLine("ActionExecutingContext.....");
logger.LogInformation(context.HttpContext.Request.Path + "before..");
this.student.DoHomeWork(write);
}
ActionExecutedContext executed = await next();
{
Console.WriteLine("ActionExecutedContext.....");
logger.LogInformation(context.HttpContext.Request.Path + "after..");
}
}
}
}
全局註冊
Program.cs
//全局註冊
builder.Services.AddControllersWithViews(options =>
{
options.Filters.Add<CustomAsyncActionFilter>();
});