using System.Reflection;
using Fw.Utility.UnifyResponse;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
namespace XF.Domain.Filters
{
public class UnifyResponseFilter : IActionFilter
{
///
/// Called before the action executes, after model binding is complete.
///
/// The .
public void OnActionExecuting(ActionExecutingContext context)
{
}
///
/// Called after the action executes, before the action result.
///
/// The .
public void OnActionExecuted(ActionExecutedContext context)
{
if (context.Canceled || context.Exception != null)
{
return;
}
// if (context.ActionDescriptor is ControllerActionDescriptor action)
// {
// var origin = action?.MethodInfo.GetCustomAttribute(typeof(OriginResponseAttribute));
// if (origin is not null) return;
// }
switch (context.Result)
{
case JsonResult jsonResult:
if (jsonResult.Value is not null && jsonResult.Value is not ApiResponse)
{
jsonResult.Value = ApiResponse