Why does ASP.NET Web API allow only one parameter for POST method?

左心房为你撑大大i 提交于 2019-12-12 14:47:23

问题


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.


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/19424493/why-does-asp-net-web-api-allow-only-one-parameter-for-post-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!