0

I have an action in Web API with a route like the following:

/users/{userId}/friends

And I want to post a value to it with the following action:

[ActionName("friends")]
        public IHttpActionResult Friends2(string userId, [FromBody]string friendId)

I am posting JSON like:

{ "friendId": "123" }

However, the friendId is always null. I think this is because Web API only allows 1 parameter when specifying [FromBody].

So how can I post to this action with the userId coming from the querystring and the friendId coming as JSON in the body?

1 Answer 1

2

You could use a view model that your controller action will take as parameter and bind to the JSON structure sent in the body:

public class MyViewModel
{
    public string FriendId { get; set; }
}

and then:

ActionName("friends")]
public IHttpActionResult Friends2(string userId, MyViewModel model)
{
    ...
}
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.