Postman with miltiple params on Web Api C#

丶灬走出姿态 提交于 2019-12-31 03:48:09

问题


I'm having a problem with this technology,

I already have created a entry in my web api controller that allows me to create users:

public IHttpActionResult PostUser(User user)

and I can consume this rest service with postman like this:

Now I want to create a similar entry but this time with 2 parameters, like this:

        public IHttpActionResult PutUser(int id, User user)

MY PROBLEM IS THAT I CAN'T REACH THIS METHOD WITH POSTMAN

I already try:

  • Add my "id" parameter on postman heather
  • Add my "id" parameter on postman body-form-data
  • Add my "id" parameter on postman body-x-www-form-urlencoded
  • Add my "id" parameter on postman body-raw befor and after the json code using as separator ['&' , ','] and as asignation char [':', '='].

none of these worked and I ran out of ideas.

does anyone knows how to invoke this service properly ?

best regards!


回答1:


You should add attributes to your method, as such:

public IHttpActionResult PutUser(int id, [FromBody] User user)

This indicates the user parameter is received from the request body. Next, you can use the following URL:

http://localhost:15423/api/users?id=<your_id>

And in favor of full on REST use, if you add [FromUri] to the id parameter, and a [Route("{id}")] to the method, you could use:

http://localhost:15423/api/users/5

Where 5 can be replaced by your id.

(Both requests should include the User object in the body)



来源:https://stackoverflow.com/questions/39681590/postman-with-miltiple-params-on-web-api-c-sharp

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