0

I have a special case when I need to get some data from request body (or model let say) inside action filter (AuthorizationFilterAttribute). I have found this way:

public async override Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
    var model = await actionContext.Request.Content.ReadAsAsync<XYZ>();
    var valueINeed = model.Something;
    etc...
}

This works fine but unfortunately after calling ReadAsAsync<> once it is not possible to read model again (I guess that ReadAsAsync<> move position in underlining stream). Because it can't be read again model not make it into controller action:

public async Task<HttpResponseMessage> Put([FromBody]XYZ order)
{
    // order is null here
}

Any thoughts how to read model in action filter or how to work around this problem?

Michal
  • 1,324
  • 2
  • 16
  • 38

1 Answers1

1

In Web API, the response body is a read forward only stream. So once you read it, you cannot read it again.

Consider passing the something in query params or some other parameter, other than body.

Community
  • 1
  • 1
Mrchief
  • 75,126
  • 20
  • 142
  • 189