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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.