Web API ActionFilter修改返回值 [英] Web API ActionFilter modify returned value

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

问题描述

我有一个Web API应用程序,我需要通过ActionFilter的OnActionExecuted方法获取某些API端点的返回值

I have a Web API application that I need to get ahold of the return value of some of the API endpoints via an ActionFilter's OnActionExecuted method

我正在使用自定义属性来标识具有需要修改的数据的端点,但是我似乎无法从HttpActionExecutedContext中找到实际的结果对象.

I'm using a custom attribute to identify the endpoints that have data that I need to modify, but I can't seem to find the actual result object from within the HttpActionExecutedContext.

感谢您的帮助!

推荐答案

您可以通过 Response.Content 属性获取返回值.如果您的操作返回了一个对象,则可以将其强制转换为 ObjectContent ,从那里您可以获取返回值的实际实例:

You can get the returned value through the Response.Content property. If your action has returned an object you can cast it to ObjectContent from where you can get the actual instance of the returned value:

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
        }
    }
}

这篇关于Web API ActionFilter修改返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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