在我的控制器中,JsonResult方法如下所示:
public JsonResult MyTestJsonB()
{
return Json(new {Name = "John", Age = "18", DateOfBirth = DateTime.UtcNow}, "text/plain", JsonRequestBehavior.AllowGet);
}在下面的属性类的OnResultExecuted方法中.
public class JsonResultAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
....
}
}我希望能够按以下方式解析filterContext。我如何完成以下工作?
发布于 2016-11-03 00:35:09
您可以使用System.Reflection实现这一点。
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
}
}
}
}
}不要忘记在您的操作中添加[JsonResultAttribute]。
https://stackoverflow.com/questions/23280846
复制相似问题