24

ActionFilter の OnActionExecuted メソッドを介して、いくつかの API エンドポイントの戻り値を取得する必要がある Web API アプリケーションがあります。

カスタム属性を使用して、変更が必要なデータを持つエンドポイントを識別していますが、HttpActionExecutedContext 内から実際の結果オブジェクトを見つけることができないようです。

助けてくれてありがとう!

4

1 に答える 1

45

プロパティを介して戻り値を取得できResponse.Contentます。アクションがオブジェクトを返した場合は、そのオブジェクトをキャストしてObjectContent、戻り値の実際のインスタンスを取得できます。

public class MyFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext context)
    {
        var objectContent = context.Response.Content as ObjectContent;
        if (objectContent != null)
        {
            var type = objectContent.ObjectType; //type of the returned object
            var value = objectContent.Value; //holding the returned value
        }
    }
}
于 2012-10-06T14:49:51.530 に答える