3

I am new to ASP.NET Web API and I found that it has one limitation which is very annoying. It only supports one parameter to post method. (Read more here: Using jQuery to POST [FromBody] parameters to Web API)

From that link, it seems like they design it this way. It doesn't make any sense to me to have such strange limitation.

If anyone knows why it was designed this way, please let me know.

2 Answers 2

3

This is the way HTTP works. An HTTP POST sends a body. And as a web api, that body represents the resource object that is being POSTed.

That 'object' which is in the POST can be a complex object with many properties so you can create a wrapper object to represent the resource. It can also be an array of objects as a collection of resources.

As a side note, if you're designing a RESTful web api, then it's desirable to POST an single resource or array of resources (objects) to an endpoint that represents a collection of resources. For example, I post a student to /api/students and it adds that student.

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

2 Comments

Cant we create public string Post([FromBody]string value, int someNo) and in jQuery data:{value:searializedForm, someNo:24} in web api?
{value:form, someNo:24} is a JSON representation of a single object with two properties; value and someNo.
2

You can have many parameters but you can only have one [FromBody], and that's because an HTTP message can only have one body. You can map as many parameters as you like to parameters on the query string.

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.