一、使用演示
- 创建
CustomResourceFilter
实现类
csharp
using Microsoft.AspNetCore.Mvc.Filters;
namespace StartUp.Filters
{
public class CustomResourceFilter : Attribute, IResourceFilter
{
/// <summary>
/// 在xxx资源之前
/// </summary>
/// <param name="context"></param>
public void OnResourceExecuting(ResourceExecutingContext context)
{
Console.WriteLine("before");
}
/// <summary>
/// 在xxx资源之后
/// </summary>
/// <param name="context"></param>
public void OnResourceExecuted(ResourceExecutedContext context)
{
Console.WriteLine("after");
}
}
}
- 在需要过滤的方法上添加注解
[CustomResourceFilter]
,填写参数名为刚刚创建的类名
- 测试
二、应用场景------缓存
2.1 演示代码
csharp
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace StartUp.Filters
{
public class CustomResourceFilter : Attribute, IResourceFilter
{
// 定义一个缓存区域
private static Dictionary<string,object> cache = new Dictionary<string, object>();
/// <summary>
/// 在xxx资源之前
/// </summary>
/// <param name="context"></param>
public void OnResourceExecuting(ResourceExecutingContext context)
{
// 如果有缓存,则返回缓存值
string key = context.HttpContext.Request.Path;
if (cache.ContainsKey(key))
{
Console.WriteLine("有缓存了,调用缓存");
// 只要是给result赋值了,就会终止执行,直接返回结果给调用方
context.Result = (IActionResult?)cache[key];
}
}
/// <summary>
/// 在xxx资源之后
/// </summary>
/// <param name="context"></param>
public void OnResourceExecuted(ResourceExecutedContext context)
{
Console.WriteLine("方法已经执行了,将结果存入缓存");
string key = context.HttpContext.Request.Path;
// 计算结果保存到缓存中
cache[key] = context.Result;
}
}
}
2.2 测试
三、 异步版本 IAsyncResourceFilter
3.1 演示代码
csharp
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace StartUp.Filters
{
public class CustomResourceFilter : Attribute, IAsyncResourceFilter
{
/// <summary>
/// 当xxx资源去执行时
/// </summary>
/// <param name="context"></param>
/// <param name="next"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
await Console.Out.WriteLineAsync("异步方法执行前");
ResourceExecutedContext resourceExecutedContext = await next.Invoke();
await Console.Out.WriteLineAsync("异步方法执行后");
}
}
}
3.2 代码改造
csharp
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace StartUp.Filters
{
public class CustomResourceFilter : Attribute, IAsyncResourceFilter
{
// 定义一个缓存区域
private static Dictionary<string, object> cache = new Dictionary<string, object>();
/// <summary>
/// 当xxx资源去执行时
/// </summary>
/// <param name="context"></param>
/// <param name="next"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
string key = context.HttpContext.Request.Path;
if (cache.ContainsKey(key))
{
Console.WriteLine("有缓存了,调用缓存");
// 只要是给result赋值了,就会终止执行,直接返回结果给调用方
context.Result = (IActionResult?)cache[key];
}
else
{
ResourceExecutedContext resourceExecutedContext = await next.Invoke();
Console.WriteLine("方法已经执行了,将结果存入缓存");
// 计算结果保存到缓存中
cache[key] = context.Result;
}
}
}
}