的.NET Web API IActionFilter.OnActionExecuted返回类型 [英] .Net Web API IActionFilter.OnActionExecuted return type

查看:344
本文介绍了的.NET Web API IActionFilter.OnActionExecuted返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对的.NET Web API的应用程序。每个动作得到执行后,我想看看结果,并改变一些东西在。

I have an application on .Net Web API. After each action get executed, I want to take a look at result and change something in that.

说我的API controllerAction看起来像

Say my API controllerAction looks like

public Car Get()
{
  ...
}

在执行行动

之后,我想修改的返回类型的属性,在这种情况下,汽车(但可以为不同的操作有所不同)。

After the action is executed, I want to modify the properties of return type in this case Car (but can be different for different action).

我知道IActionFilter.OnActionExecuted()动作被执行之后被调用。但我不知道如何访问该方法的返回类型。

I know that IActionFilter.OnActionExecuted() gets called after an action gets executed. But I am not sure how to access the return type in this method.

推荐答案

您应该能够通过查看在行动执行方面的反应要做到这一点。如果你想获得汽车,并修改它,你可以这样写:

You should be able to do this by looking at the response on the action executed context. If you want to get the car and modify it you could write something like this:

Car car;
if (actionExecutedContext.Response.TryGetContentValue<Car>(out car))
{
    // modify the car to send back in the response
}

如果您要检查响应的类型,你可以写:

If you want to check the type of the response, you could write:

ObjectContent objectContent = actionExecutedContext.Response.Content as ObjectContent;
if (objectContent != null)
{
    Type responseType = objectContent.ObjectType;
    // do stuff with the type
}

这篇关于的.NET Web API IActionFilter.OnActionExecuted返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆