从json中的actionfilter HttpActionContext和HttpActionExecutedContext读取数据

时间:2018-09-15 13:00:13

标签: c# asp.net-web-api2

我在提取请求和响应数据时遇到问题。我尝试下面的代码,它工作正常。只想知道是否有更好的方法可以在动作过滤器webapi中获取请求和响应数据。

public override void OnActionExecuted(HttpActionContext actionContext)
{
    string Request;
    using (var stream = new StreamReader(actionContext.Request.Content.ReadAsStreamAsync().Result))
    {
        stream.BaseStream.Position = 0;
        Request = stream.ReadToEnd();
    }
}

public override void OnActionExecuting(HttpActionContext filterContext)
{
    string Request;
    using (var ms = new MemoryStream())
    {
        var stream = new StreamReader(filterContext.Request.Content.ReadAsStreamAsync().Result);
        stream.BaseStream.Position = 0;
        Request = stream.ReadToEnd();
    }                
}

0 个答案:

没有答案