【发布时间】:2018-02-27 16:40:20
【问题描述】:
【问题讨论】:
-
在监视窗口中按原样投射:((Microsoft.AspNetCore.Mvc.ObjectResult) context.Result).Value
标签: c# .net asp.net-core httpcontext
【问题讨论】:
标签: c# .net asp.net-core httpcontext
使用System.Reflection 和JsonResult 来达到您想要的值。还将 [JsonResultAttribute] 添加到您的控制器操作中。
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
}
}
}
}
}
【讨论】: