14

Below is WebAPI action. On googling about the below error:-

The requested resource does not support http method 'POST'

I got number of links & updated my api accordingly but still I am getting the same error.

But still when calling the above via post man throws the error.

Error

How do I get rid of this error??

Also is it possible to fix this without using [FromBody] attribute in the method parameters list?

Any help/suggestion highly appreciated. Thanks.

Community
  • 1
  • 1
Kgn-web
  • 7,047
  • 24
  • 95
  • 161

3 Answers3

9

You have declared route which requires url parameters

[Route("rename/{userId}/{type}/{title}/")]

So when you send request to api/customer/rename it does not match this method. You should remove parameters which you are passing in request body from route parameters

[Route("rename")]

Make sure that you have appropriate RoutePrefix("api/customer") attribute on your controller.


Second problem is multiple [FromBody] parameters. You will get can't bind multiple parameters error. There is limitation - you can mark only one parameter as FromBody. See Sending Simple Types notes:

Web API reads the request body at most once, so only one parameter of an action can come from the request body. If you need to get multiple values from the request body, define a complex type.

You should create complex type which will hold all parameters

public class RenameModel
{
   public int UserId { get; set; }
   public string Type { get; set; }
   public string Title { get; set; }
}

And change method signature to

[HttpPost]
[Route("rename")]
public IHttpActionResult Rename(RenameModel model)

And send request data as application/x-www-form-urlencoded

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
2
 [Route("rename/{userId}/{type}/{title}/")]
 public IHttpActionResult Rename([FromBody] int userId,  [FromBody] string  type, [FromBody] string title)

The last answer is correct, you're asking for these parameters in the route, but saying that you expect them in the post body. Also, usually the route would begin with a noun rather than a verb. What is it you're renaming? (i.e. [Route("users/rename/{userId}/{type}/{title}")]

Based on your initial post, try this instead:

 [HttpPost]
 [Route("rename/{userId}/{type}/{title}" Name = "RenameUser"]
 public IHttpActionResult Rename(int userId, string type, string title)
 {
     _myServiceMethod.Rename(userId, type, title);
     return new StatusCodeResult(HttpStatusCode.Created, this);   
 }

Or, if you wanted to do a post with the info in the body: Declare your data contract:

public class User
{
    public string Type { get; set; }
    public string Title { get; set; }
}

Then on the endpoint:

[HttpPost]
[Route("rename/{userId}", Name = "RenameUserPost")]
public IHttpActionResult RenameUserPost(int userId, [FromBody] User userData)
{
    return new StatusCodeResult(HttpStatusCode.Created, this);
}

Note that in both returns 'this' refers to your controller class that inherits from ApiController. Verified both of these in swagger, and they accept POSTs and return status codes.

Hope this helps.

jeffj23
  • 171
  • 1
  • 9
  • What is the use of Name attribute ?? – Kgn-web Feb 15 '17 at 14:42
  • Multiple [FromBody] is fine or it could be a cause?? – Kgn-web Feb 15 '17 at 14:48
  • Route naming is used for generating links, if, for example you wanted to use the Created(Uri location, T Content) response type, you could use either a hand rolled route helper or something like Ploeh.Hyprlinkr to get the Uris dynamically by route name. Thus if you change the route, the link will still be correct. – jeffj23 Feb 15 '17 at 14:52
  • I don't believe multiple FromBody attributes are allowed. This could be the direct cause of your route not being picked up. That said, I haven't run into an instance where I've ever needed to post multiple disparate objects. Your route should handle one thing only. In your particular case, creating the data contract like the second example above should work fine from you. – jeffj23 Feb 15 '17 at 14:56
0

I had this error for wrong string in Route string on top of my action.

[Route("api/TestReaderPercentStudyHomework/AddOrUpdate")]

M Komaei
  • 7,006
  • 2
  • 28
  • 34