Why does ASP.NET Web API allow only one parameter

2019-07-04 00:40发布

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条回答
干净又极端
2楼-- · 2019-07-04 00:50

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.

查看更多
趁早两清
3楼-- · 2019-07-04 01:01

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.

查看更多
登录 后发表回答