目录
一:说明
IResultFilter同步过滤器与IAsyncResultFilter异步过滤器常常被用作于渲染视图或处理结果。
IResultFilter同步过滤器执行顺序:
1:执行控制器中的构造函数,实例化控制器
2:执行具体的Action方法
3:执行ResultFilter.OnResultExecuting方法
4:渲染视图或处理结果
5:执行ResultFilter.OnResultExecuted方法
IActionFilter同步过滤器与IAsyncActionFilter异步过滤器处理Json数据或Ajax数据。
二:IResultFilter同步
控制器代码:
/// <summary>
/// Get请求
/// 应用ResultFilter扩展
/// Home控制器的Index
/// </summary>
/// <returns>Index视图</returns>
[HttpGet]
[ResultFilter]
public IActionResult Index()
{
return Json(new
{
Id = 123456,
Name = "Vin Cente",
Age = 28
});
}
关键类ResultFilter代码:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Study_ASP.NET_Core_MVC.Models;
namespace Study_ASP.NET_Core_MVC.Utility.Filters
{
public class ResultFilter : Attribute, IResultFilter
{
/// <summary>
/// 在XXX执行之前
/// </summary>
/// <param name="context"></param>
/// <exception cref="NotImplementedException"></exception>
public void OnResultExecuting(ResultExecutingContext context)
{
if(context.Result is JsonResult)
{
JsonResult result = (JsonResult)context.Result;
context.Result = new JsonResult(new AjaxResult()
{
Success = true,
Message="Ok",
Data = result.Value
});
}
}
/// <summary>
/// 在XXX执行之后
/// </summary>
/// <param name="context"></param>
/// <exception cref="NotImplementedException"></exception>
public void OnResultExecuted(ResultExecutedContext context)
{
Console.WriteLine("在XXX执行之后ResultFilter.OnResultExecuted方法");
}
}
}
关键AjaxResult类:
namespace Study_ASP.NET_Core_MVC.Models
{
public class AjaxResult
{
/// <summary>
/// 初始化结果
/// </summary>
public bool Success { get; set; }
/// <summary>
/// 初始化结果信息
/// </summary>
public string? Message { get; set; }
/// <summary>
/// 初始化结果数据
/// </summary>
public object? Data { get; set; }
}
}
效果截图:
三:IAsyncResultFilter异步
控制器代码:
/// <summary>
/// Get请求
/// 应用ResultAsyncFilter扩展
/// Home控制器的Index
/// </summary>
/// <returns>Index视图</returns>
[HttpGet]
[ResultAsyncFilter]
public IActionResult Index()
{
return Json(new
{
Id = 123456,
Name = "Vin Cente",
Age = 28
});
}
关键类ResultAsyncFilter代码:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Study_ASP.NET_Core_MVC.Models;
namespace Study_ASP.NET_Core_MVC.Utility.Filters
{
public class ResultAsyncFilter : Attribute, IAsyncResultFilter
{
/// <summary>
/// 在XXX执行时
/// </summary>
/// <param name="context"></param>
/// <param name="next"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
if (context.Result is JsonResult)
{
JsonResult result = (JsonResult)context.Result;
context.Result = new JsonResult(new AjaxResult()
{
Success = true,
Message = "Ok",
Data = result.Value
});
}
await next.Invoke();
}
}
}
关键AjaxResult类:
namespace Study_ASP.NET_Core_MVC.Models
{
public class AjaxResult
{
/// <summary>
/// 初始化结果
/// </summary>
public bool Success { get; set; }
/// <summary>
/// 初始化结果信息
/// </summary>
public string? Message { get; set; }
/// <summary>
/// 初始化结果数据
/// </summary>
public object? Data { get; set; }
}
}
效果截图: