6

I've been creating a WebApi controller, and I'm trying to use a HttpPost request to submit a new user for registration. This works fine on my localhost, but when I publish it to Azure I get a 405 method not allowed error with the message: "The requested resource does not support http method 'GET'".

I use postman to check the actions, and so I used generate code to see the request, which is as follows:

POST /api/account/register/student HTTP/1.1
Host: www.l3cture.com
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 27c1b2ab-96ad-4a99-b271-4030402768e7

So I can clearly see that the request is a POST. And the following is the controller action code with its attributes (I've currently simplified it so that no models are posted, and the same behaviour occurs)

[HttpPost]
[Route("register/student")]
[AllowAnonymous]
public async Task<IHttpActionResult> PostStudent(/*RegisterStudent model*/)
{
    //Implementation of register
    return Ok();
}

I've checked the namespace of the attributes, and it is System.Web.Http, so it's not being confused with the MVC namespace.

The interesting thing is that when I change the method to be HttpGet, and POST to it using postman, I receive a status 200. It's almost like the HttpPost requests are all being treated like HttpGet by my controller.

I've used HttpPut and HttpDelete in other places, and they all work fine.

I'm unsure how to tackle this problem, and was wondering if anyone had any ideas? Please let me know if I need to post more code for clarification.

Thanks in advance

A.Walsh
  • 211
  • 2
  • 7
  • 1
    Seems to work when I use https instead of http. This can be closed – A.Walsh Aug 12 '16 at 17:06
  • Are you passing your model in Body for Post? Think you are missing [FromBody] attribute in your method parameter. – Ankit Vijay Aug 12 '16 at 17:06
  • I'd tried [FromBody] and it hadn't worked. Although i just tried it by specifying https, and it seems to work. I must've just been an idiot for a while – A.Walsh Aug 12 '16 at 17:08

1 Answers1

15

Turns out I was using Http, instead of Https.

A.Walsh
  • 211
  • 2
  • 7
  • If this was really all there was to your issue, you should accept this answer (checkmark on the left) to let other users that might find your question that it's the solution. – Francis Lord Aug 12 '16 at 18:36
  • Yeah sorry, I couldn't when I posted, needed to wait two days. Thanks for reminding me – A.Walsh Aug 15 '16 at 19:31
  • Oh my god! You saved my day! 6 hours searching for solution on the web, and the problem was that I forgot to put https in the request URL. Thank you!!!!!!! – Federico Mastrini Mar 03 '18 at 16:42
  • Thanks you! This saved me more frustration, although I already spent a lot of it! – CBrown77 Jul 03 '18 at 13:48
  • For me it was 'www.' vs no 'www.'. Adding the www resolved. – flashsplat Aug 17 '20 at 13:18