-1

How can I get values from ResultExecutedContext result? In the debug I can see there are values: enter image description here

But the intelisense doesn't see it.

mskuratowski
  • 4,014
  • 15
  • 58
  • 109

1 Answers1

0

Use System.Reflection and JsonResult to reach the values you want. Also add [JsonResultAttribute] to your controller action.

public class JsonResultAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {

        // Detect that the result is of type JsonResult
        if (filterContext.Result is JsonResult)
        {
            var jsonResult = filterContext.Result as JsonResult;

            // Dig into the Data Property
            foreach(var prop in jsonResult.Value.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                var propName = prop.Name;
                var propValue = prop.GetValue(jsonResult.Value,null);

                Console.WriteLine("Property: {0}, Value: {1}",propName,     propValue);

                // Detect if property is an DateTime
                if (propValue is DateTime)
                {
                    // Take some action
                }
            }
        }
    }
}
AmiNadimi
  • 5,129
  • 3
  • 39
  • 55