Postman with miltiple params on Web Api C#

2019-03-01 00:28发布

问题:

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)