//全局过滤器
builder.Services.AddMvc(m =>
{
??? m.Filters.Add<AllResultFilter>();
});
1、实现过滤器
public class AllResultFilter : IResultFilter
{
/// <summary>
/// 结果执行后方法
/// 不可更改结果
/// </summary>
/// <param name="context"></param>
/// <exception cref="NotImplementedException"></exception>
public void OnResultExecuted(ResultExecutedContext context)
{
throw new Exception("ce");
Trace.WriteLine("OnResultExecuted之后");
}
/// <summary>
/// 结果执行之前
/// 可更改结果
/// </summary>
/// <param name="context"></param>
/// <exception cref="NotImplementedException"></exception>
public void OnResultExecuting(ResultExecutingContext context)
{
Trace.WriteLine("OnResultExecuted之前");
}
}
2、结论
OnResultExecuting可以修改返回结果,相当于在方法目标执行完追加执行
OnResultExecuted不可以修改返回结果,相当于目标方法执行返回结果后执行