我在.Net Web API上有一个应用程序。在每个动作执行完毕后,我想看看结果并改变其中的一些内容。
说我的API controllerAction看起来像
public Car Get()
{
...
}
执行操作后,我想在这种情况下修改返回类型的属性Car(但对于不同的操作可能会有所不同)。
我知道在执行一个动作后会调用IActionFilter.OnActionExecuted()。但我不知道如何在此方法中访问返回类型。
答案 0 :(得分:3)
您应该能够通过查看操作执行上下文的响应来执行此操作。如果你想要开车并修改它,你可以这样写:
Car car;
if (actionExecutedContext.Response.TryGetContentValue<Car>(out car))
{
// modify the car to send back in the response
}
如果要检查响应的类型,可以写:
ObjectContent objectContent = actionExecutedContext.Response.Content as ObjectContent;
if (objectContent != null)
{
Type responseType = objectContent.ObjectType;
// do stuff with the type
}