Reputation: 7092
I'm trying to get an api method to use POST but it insists on trying to use GET. I've Googled and searched SO, but everything I try just returns the same message.
Here's my controller:
[Route("api/game/{gameId}/createcharacter/{name}")]
[AcceptVerbs("POST")]
[HttpPost]
public IHttpActionResult PostNewCharacter([FromBody]string name)
{
return Created("created", CharacterGenerationService.CreateNewCharacter(name));
}
Here's the message I get no matter what I try:
message: "The requested resource does not support http method 'GET'."
Request URL:http://localhost:61645/api/game/452/createcharacter/testChar1 Request Method:GET Status Code:405 Method Not Allowed Remote Address:[::1]:61645
I am using : using System.Web.Http;
Is there a trick to this?
Thanks
Upvotes: 2
Views: 109
Reputation: 990
use this code:
[Route("api/game/{gameId}/createcharacter/{name}")]
[HttpPost]
public IHttpActionResult PostNewCharacter(string name)
{
return Created("created", CharacterGenerationService.CreateNewCharacter(name));
}
Upvotes: 2
Reputation: 23190
In the route template you said you will get the name
parameter value from the URL but in the signature of your method your decorate that same parameter with FromBody
attribute which tell that the value will be retrieved from the message body.
You have two choices :
FromBody
attribute and your URL will work correctlyname
value into your request body. What about the gameId
parameter ?
Don't you need it in your method ? If yes, then you need to pass it as a method parameter.
Upvotes: 0