我期待创建一个实现IAsyncActionFilter
的过滤器,它将从当前请求的上下文的ActionParameters
及其Result
中检索数据。我正在使用自定义属性MyLogAttribute
来指导伐木行为,例如选择加入日志并显示包含关键信息的字段。ASP.NET核心AsyncActionFilter记录ActionArguments和结果
public class AsyncMyLogFilter : IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
if (actionDescriptor != null)
{
var attribute = actionDescriptor.MethodInfo.GetCustomAttribute<MyLogAttribute>();
if (attribute != null)
{
await next();
// This is where the magic is supposed to happen:
LoggerHelper.Log(context.ActionArguments, context.Result);
}
return;
}
await next();
}
}
方式的滤波器提供next()
委托使我相信,过去的这一点上,行动将完成,可用于检查作为ObjectResult
结果对象。但是,尽管过滤器能够毫无问题地获取ActionArguments
,但不幸的是Result
属性为空,这根本没有帮助。
明显的替代方案,同步IActionFilter
,让我检查的OnActionExecuted
阶段Result
对象,但在这一点上ActionArguments
字典不可用。
那么,有没有办法在相同的方法范围内访问ActionArguments
和Result
?
-S
参数在ActionExecutedContext.ActionDescriptor.Parameters?上不可用吗? –
Parameters集合包含参数的名称和类型,但似乎不包含它们的值。 –